src/server/util/util.c

Sat, 24 Aug 2024 22:37:12 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 24 Aug 2024 22:37:12 +0200
changeset 556
b036ccad4b49
parent 548
40ecc0a6b280
permissions
-rw-r--r--

improve webserver shutdown and free some stuff to make the valgrind output cleaner

1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * THE BSD LICENSE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * modification, are permitted provided that the following conditions are met:
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 * Redistributions of source code must retain the above copyright notice, this
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * list of conditions and the following disclaimer.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * Redistributions in binary form must reproduce the above copyright notice,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * this list of conditions and the following disclaimer in the documentation
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 * and/or other materials provided with the distribution.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * Neither the name of the nor the names of its contributors may be
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * used to endorse or promote products derived from this software without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * specific prior written permission.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 * util.c: A hodge podge of utility functions and standard functions which
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 * are unavailable on certain systems
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 * Rob McCool
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #ifdef XP_UNIX
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <sys/types.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include <sys/wait.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include <stdlib.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #include <unistd.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #include <limits.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #include "prthread.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 #endif /* XP_UNIX */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
24
1a7853a4257e removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
51 //include "nspr.h"
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
52 #include <errno.h>
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
53
24
1a7853a4257e removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
54 #include "../daemon/netsite.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
55 #include "../public/nsapi.h"
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
56 #include <cx/string.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
57 #include <cx/mempool.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
58 #include <cx/utils.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
59 #include <cx/printf.h>
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
61 #include "pblock.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 #include "util.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
127
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
64 #include <openssl/bio.h>
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
65 #include <openssl/buffer.h>
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
66 #include <openssl/evp.h>
62
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
67
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
68
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
69 /* ------------------------------ _uudecode ------------------------------- */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
70
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
71 static const unsigned char pr2six[256] = {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
72 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
73 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
74 52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
75 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
76 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
77 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
78 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
79 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
80 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
81 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
82 64,64,64,64,64,64,64,64,64,64,64,64,64
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
83 };
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
84
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
85 /** you MUST reserve at least 2 additional bytes for bufout */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
86 size_t util_base64decode(char *bufcoded, size_t codedbytes, char *bufout) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
87 register char *bufin = bufcoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
88 register int nprbytes;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
89 size_t nbytesdecoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
90
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
91 /* Find the length */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
92 nprbytes = (int) codedbytes;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
93 while(pr2six[(int)(bufin[nprbytes-1])] >= 64) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
94 nprbytes--;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
95 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
96 nbytesdecoded = ((nprbytes+3)/4) * 3;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
97
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
98 while (nprbytes > 0) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
99 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
100 (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
101 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
102 (pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
103 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
104 (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
105 bufin += 4;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
106 nprbytes -= 4;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
107 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
108
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
109 if(nprbytes & 03) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
110 if(pr2six[(int)bufin[-2]] > 63)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
111 nbytesdecoded -= 2;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
112 else
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
113 nbytesdecoded -= 1;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
114 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
115
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
116 return nbytesdecoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
117 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
118
127
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
119 char* util_base64encode(char *in, size_t len) {
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
120 BIO *b;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
121 BIO *e;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
122 BUF_MEM *mem;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
123
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
124 e = BIO_new(BIO_f_base64());
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
125 b = BIO_new(BIO_s_mem());
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
126 BIO_set_flags(e, BIO_FLAGS_BASE64_NO_NL);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
127
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
128 e = BIO_push(e, b);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
129 BIO_write(e, in, len);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
130 BIO_flush(e);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
131
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
132 BIO_get_mem_ptr(e, &mem);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
133 char *out = malloc(mem->length + 1);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
134 memcpy(out, mem->data, mem->length);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
135 out[mem->length] = '\0';
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
136
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
137 BIO_free_all(e);
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
138
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
139 return out;
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
140 }
84e206063b64 adds minimal websocket implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
141
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
142
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
143 /* --------------------------- util_env_create ---------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
144
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
145
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
146 NSAPI_PUBLIC char **util_env_create(char **env, int n, int *pos)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
147 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
148 int x;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
149
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
150 if(!env) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
151 *pos = 0;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
152 return (char **) MALLOC((n + 1)*sizeof(char *));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
153 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
154 else {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
155 for(x = 0; (env[x]); x++);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
156 env = (char **) REALLOC(env, (n + x + 1)*(sizeof(char *)));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
157 *pos = x;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
158 return env;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
159 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
160 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
161
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
162
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
163 /* ---------------------------- util_env_free ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
164
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
165
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
166 NSAPI_PUBLIC void util_env_free(char **env)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
167 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
168 register char **ep = env;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
169
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
170 for(ep = env; *ep; ep++)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
171 FREE(*ep);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
172 FREE(env);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
173 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
174
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
175 /* ----------------------------- util_env_str ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
176
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
177
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
178 NSAPI_PUBLIC char *util_env_str(const char *name, const char *value) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
179 char *t;
118
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
180
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
181 size_t len = strlen(name) + strlen(value) + 2;
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
182 t = (char *) MALLOC(len); /* 2: '=' and '\0' */
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
183
118
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
184 snprintf(t, len, "%s=%s", name, value);
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
185
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
186 return t;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
187 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
188
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
189
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
190 /* --------------------------- util_env_replace --------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
191
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
192
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
193 NSAPI_PUBLIC void util_env_replace(char **env, const char *name, const char *value)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
194 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
195 int x, y, z;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
196 char *i;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
197
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
198 for(x = 0; env[x]; x++) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
199 i = strchr(env[x], '=');
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
200 *i = '\0';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
201 if(!strcmp(env[x], name)) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
202 y = strlen(env[x]);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
203 z = strlen(value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
204
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
205 env[x] = (char *) REALLOC(env[x], y + z + 2);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
206 util_sprintf(&env[x][y], "=%s", value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
207 return;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
208 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
209 *i = '=';
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
211 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
212
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
213
118
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
214 /* ---------------------------- util_sh_escape ---------------------------- */
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
215
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
216
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
217 NSAPI_PUBLIC char *util_sh_escape(char *s)
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
218 {
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
219 char *ns = (char *) MALLOC(strlen(s) * 2 + 1); /* worst case */
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
220 register char *t, *u;
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
221
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
222 for(t = s, u = ns; *t; ++t, ++u) {
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
223 if(strchr("&;`'\"|*?~<>^()[]{}$\\ #!", *t))
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
224 *u++ = '\\';
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
225 *u = *t;
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
226 }
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
227 *u = '\0';
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
228 return ns;
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
229 }
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
230
38bf6dd8f4e7 adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
231
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
232 /* ---------------------------- util_env_find ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
233
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
234
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
235 NSAPI_PUBLIC char *util_env_find(char **env, const char *name)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
236 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
237 char *i;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
238 int x, r;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
239
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
240 for(x = 0; env[x]; x++) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
241 i = strchr(env[x], '=');
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
242 *i = '\0';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
243 r = !strcmp(env[x], name);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
244 *i = '=';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
245 if(r)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
246 return i + 1;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
248 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
249 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
250
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
251
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
252 /* ---------------------------- util_env_copy ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
253
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
254
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
255 NSAPI_PUBLIC char **util_env_copy(char **src, char **dst)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
256 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
257 char **src_ptr;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
258 int src_cnt;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
259 int index;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
260
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
261 if (!src)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
262 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
263
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
264 for (src_cnt = 0, src_ptr = src; *src_ptr; src_ptr++, src_cnt++);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
265
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
266 if (!src_cnt)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
267 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
268
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
269 dst = util_env_create(dst, src_cnt, &index);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
270
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
271 for (src_ptr = src; *src_ptr; index++, src_ptr++)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
272 dst[index] = STRDUP(*src_ptr);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
273 dst[index] = NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
274
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
275 return dst;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
277
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
278 /* ----------------------------- util_sprintf ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
279
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
280 NSAPI_PUBLIC int util_vsnprintf(char *s, int n, register const char *fmt,
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
281 va_list args)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
282 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
283 return vsnprintf(s, n, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
284 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
285
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
286 NSAPI_PUBLIC int util_snprintf(char *s, int n, const char *fmt, ...)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
287 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
288 va_list args;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
289 va_start(args, fmt);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
290 return vsnprintf(s, n, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
291 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
292
338
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
293 NSAPI_PUBLIC int util_vasprintf(pool_handle_t *pool, char **s, const char *fmt,
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
294 va_list args)
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
295 {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
296 CxAllocator *a = pool_allocator(pool);
338
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
297 va_list ap;
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
298 va_copy(ap, args);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
299 cxmutstr str = cx_vasprintf_a(a, fmt, ap);
338
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
300 *s = str.ptr;
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
301 return str.length;
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
302 }
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
303
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
304 NSAPI_PUBLIC int util_asprintf(pool_handle_t *pool, char **s, const char *fmt, ...)
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
305 {
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
306 va_list args;
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
307 va_start(args, fmt);
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
308 return util_vasprintf(pool, s, fmt, args);
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
309 }
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
310
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
311 NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
312 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
313 return vsprintf(s, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
314 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
315
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
316 NSAPI_PUBLIC int util_sprintf(char *s, const char *fmt, ...)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
317 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
318 va_list args;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
319 va_start(args, fmt);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
320 return vsprintf(s, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
321 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
322
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
323 // TODO: asprintf
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
324
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
326
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
327 /* -------------------------- util_uri_unescape --------------------------- */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
328
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
329 NSAPI_PUBLIC void util_uri_unescape(char *s)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
330 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
331 char *t, *u;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
332
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
333 for(t = s, u = s; *t; ++t, ++u) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
334 if((*t == '%') && t[1] && t[2]) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
335 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) +
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
336 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0'));
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
337 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
338 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
339 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
340 if(u != t)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
341 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
342 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
343 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
344 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
345
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
346 /*
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
347 * Same as util_uri_unescape, but returns success/failure
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
348 */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
349 NSAPI_PUBLIC int util_uri_unescape_strict(char *s)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
350 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
351 char *t, *u, t1, t2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
352 int rv = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
353
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
354 for(t = s, u = s; *t; ++t, ++u) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
355 if (*t == '%') {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
356 t1 = t[1] & 0xdf; /* [a-f] -> [A-F] */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
357 if ((t1 < 'A' || t1 > 'F') && (t[1] < '0' || t[1] > '9'))
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
358 rv = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
359
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
360 t2 = t[2] & 0xdf; /* [a-f] -> [A-F] */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
361 if ((t2 < 'A' || t2 > 'F') && (t[2] < '0' || t[2] > '9'))
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
362 rv = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
363
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
364 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) +
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
365 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0'));
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
366 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
367 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
368 else if (u != t)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
369 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
370 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
371 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
372
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
373 return rv;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
374 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
375
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
376
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
377 NSAPI_PUBLIC int
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
378 util_uri_unescape_plus (const char *src, char *trg, int len)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
379 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
380 const char *t = src;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
381 char *u = trg == NULL ? (char *)src : trg;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
382 int rlen = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
383
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
384 if (len == -1)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
385 len = strlen (src);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
386
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
387 for( ; len && *t; ++t, ++u, len--, rlen++)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
388 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
389 if((*t == '%') && t[1] && t[2])
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
390 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
391 *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A') + 10 : (t[1] - '0')) * 16) +
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
392 (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A') + 10 : (t[2] - '0'));
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
393 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
394 len-= 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
395 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
396 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
397 if (*t == '+')
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
398 *u = ' ';
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
399 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
400 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
401 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
402 *u = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
403 return rlen;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
404 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
405
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
406
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 if(v[0] == 'T' || v[0] == 't') {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 return 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 if(v[0] == 'F' || v[0] == 'f') {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 return 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 return def;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
417 int util_getboolean_s(cxstring s, int def) {
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
418 if(s.length == 0) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
419 return def;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
420 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
421 if(s.ptr[0] == 'T' || s.ptr[0] == 't') {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
422 return 1;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
423 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
424 if(s.ptr[0] == 'F' || s.ptr[0] == 'f') {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
425 return 0;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
426 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
427 return def;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
428 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
429
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
430 NSAPI_PUBLIC int util_strtoint(const char *str, int64_t *value) {
119
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
431 char *end;
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
432 errno = 0;
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
433 int64_t val = strtoll(str, &end, 0);
548
40ecc0a6b280 fix strtoll result check
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
434 if(errno == 0 && *end == '\0') {
119
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
435 *value = val;
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
436 return 1;
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
437 } else {
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
438 return 0;
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
439 }
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
440 }
155cbab9eefd adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 118
diff changeset
441
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
442 NSAPI_PUBLIC const char* util_resource_name(const char *url) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
443 cxstring urlstr = cx_str(url);
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
444 if(urlstr.ptr[urlstr.length-1] == '/') {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
445 urlstr.length--;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
446 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
447 cxstring resname = cx_strrchr(urlstr, '/');
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
448 if(resname.length > 1) {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
449 return resname.ptr+1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
450 } else {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
451 return url;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
452 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
453 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 273
diff changeset
454
285
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
455 NSAPI_PUBLIC char* util_parent_path(const char *path) {
338
c62ea2a2133b add util_asprintf and util_vasprintf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 285
diff changeset
456 char *name = (char*)util_resource_name((char*)path);
285
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
457 size_t namelen = strlen(name);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
458 size_t pathlen = strlen(path);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
459 size_t parentlen = pathlen - namelen;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
460 char *parent = MALLOC(parentlen + 1);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
461 memcpy(parent, path, parentlen);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
462 parent[parentlen] = '\0';
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
463 return parent;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
464 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465
382
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
466 NSAPI_PUBLIC char* util_parent_path_pool(pool_handle_t *pool, const char *path) {
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
467 // maybe we can unify this function with util_parent_path
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
468 char *name = (char*)util_resource_name((char*)path);
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
469 size_t namelen = strlen(name);
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
470 size_t pathlen = strlen(path);
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
471 size_t parentlen = pathlen - namelen;
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
472 char *parent = pool_malloc(pool, parentlen + 1);
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
473 memcpy(parent, path, parentlen);
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
474 parent[parentlen] = '\0';
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
475 return parent;
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
476 }
9e2289c77b04 disable pg large object IO functions for collections
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
477
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 /* ------------------------------ util_itoa ------------------------------- */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 NSAPI_PUBLIC int util_itoa(int i, char *a)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 int len = util_i64toa(i, a);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 PR_ASSERT(len < UTIL_ITOA_SIZE);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 return len;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 NSAPI_PUBLIC int INTutil_itoa(int i, char *a) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 return INTutil_i64toa(i, a);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 /* ----------------------------- util_i64toa ------------------------------ */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 * Assumption: Reversing the digits will be faster in the general case
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 * than doing a log10 or some nasty trick to find the # of digits.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500
24
1a7853a4257e removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
501 NSAPI_PUBLIC int INTutil_i64toa(int64_t i, char *a)
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503 register int x, y, p;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 register char c;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505 int negative;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
506
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
507 negative = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
508 if(i < 0) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
509 *a++ = '-';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 negative = 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511 i = -i;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513 p = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
514 while(i > 9) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
515 a[p++] = (i%10) + '0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
516 i /= 10;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
517 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
518 a[p++] = i + '0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520 if(p > 1) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 for(x = 0, y = p - 1; x < y; ++x, --y) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522 c = a[x];
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 a[x] = a[y];
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524 a[y] = c;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
525 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
526 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527 a[p] = '\0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529 //PR_ASSERT(p + negative < UTIL_I64TOA_SIZE);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531 return p + negative;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 }
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
533
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
534
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
535
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
536 #ifndef XP_WIN32
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
537 NSAPI_PUBLIC struct passwd *
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
538 util_getpwnam(const char *name, struct passwd *result, char *buffer,
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
539 int buflen)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
540 {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
541 struct passwd *rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
542
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
543 errno = getpwnam_r(name, result, buffer, buflen, &rv);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
544 if (errno != 0)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
545 rv = NULL;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
546
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
547 return rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
548 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
549 #endif
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
550
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
551
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
552 #ifndef XP_WIN32
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
553 NSAPI_PUBLIC struct passwd *
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
554 util_getpwuid(uid_t uid, struct passwd *result, char *buffer, int buflen)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
555 {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
556 struct passwd *rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
557
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
558 errno = getpwuid_r(uid, result, buffer, buflen, &rv);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
559 if (errno != 0)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
560 rv = NULL;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
561
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
562 return rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
563 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
564 #endif
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
565
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
566
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
567 NSAPI_PUBLIC int util_errno2status(int errno_value) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
568 switch(errno_value) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
569 case 0: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
570 return 200;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
571 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
572 case EACCES: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
573 return 403;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
574 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
575 case ENOENT: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
576 return 404;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
577 break;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
578 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
579 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
580 return 500;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
581 }
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
582
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
583
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
584
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
585 NSAPI_PUBLIC
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
586 cxmutstr util_path_append(pool_handle_t *pool, char *path, char *ch) {
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
587 cxmutstr parent = cx_mutstr(path);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
588 cxmutstr child = cx_mutstr(ch);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
589 cxmutstr newstr;
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
590
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
591 CxAllocator *a = pool_allocator(pool);
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 113
diff changeset
592 if(parent.ptr[parent.length-1] == '/') {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
593 newstr = cx_strcat_a(a, 2, parent, child);
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
594 } else {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
595 newstr = cx_strcat_a(a, 3, parent, cx_str("/"), child);
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
596 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
597
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
598 return newstr;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
599 }
63
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
600
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
601 cxmutstr util_path_remove_last(cxmutstr path) {
63
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
602 int i;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
603 for(i=path.length-1;i>=0;i--) {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
604 char c = path.ptr[i];
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
605 if(c == '/') {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
606 path.ptr[i] = 0;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
607 path.length = i;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
608 break;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
609 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
610 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
611 if(i < 0) {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
612 path.ptr = NULL;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
613 path.length = 0;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
614 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
615 return path;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
616 }
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
617
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
618 void util_add_ppath(cxmutstr root, cxmutstr path, pblock *vars) {
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
619 // concat path
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
620 size_t length = root.length + path.length;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
621 char *translated_path = alloca(length);
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
622
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
623 memcpy(translated_path, root.ptr, root.length);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
624 memcpy(translated_path + root.length, path.ptr, path.length);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
625
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
626
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
627 // add path to specified pblock
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
628 pblock_kvinsert(
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
629 pb_key_ppath,
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
630 translated_path,
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
631 length,
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
632 vars);
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
633 }
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
634
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
635
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
636 // new - code from params.cpp
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
637 NSAPI_PUBLIC pblock* util_parse_param(pool_handle_t *pool, char *query) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
638 pblock *pb = pblock_create_pool(pool, 32);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
639 if(!pb) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
640 return NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
641 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
642 if(!query || !(*query)) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
643 return pb;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
644 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
645
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
646 int loopFlag = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
647 int nl = 0; // size of the name substring
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
648 int vl = 0; // size of the value substring
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
649 int state = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
650 const char *np = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
651 const char *vp = NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
652
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
653 while (loopFlag) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
654 char delim = *query++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
655 switch (delim) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
656 case '&':
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
657 case '\0': {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
658 if(!delim) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
659 loopFlag = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
660 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
661
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
662 state = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
663
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
664 if(nl > 0) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
665 util_uri_unescape_plus(np, NULL, nl);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
666 util_uri_unescape_plus(vp, NULL, vl);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
667 pblock_nvlinsert(np, nl, vp, vl, pb);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
668 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
669
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
670 nl = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
671 vl = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
672 vp = NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
673 np = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
674 break;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
675 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
676 case '=': {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
677 state = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
678 vp = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
679 break;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
680 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
681 default: {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
682 if(state) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
683 vl++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
684 } else {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
685 nl++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
686 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
687 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
688 } /* switch */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
689 } /* while */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
690
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
691 return pb;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
692 }
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
693
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
694
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
695 /* ---------------------------- util_mstr2num ----------------------------- */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
696
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
697 static const int MSTR2NUM_HT_MASK = 0xf;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
698
103
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
699 struct mstr2num_ht {
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
700 unsigned ucmstr; // Uppercase 3 character month string in a machine word
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
701 int mnum; // 0-based month number for this month string
103
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
702 };
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
703
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
704 struct mstr2num_ht MSTR2NUM_HT[] = {
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
705 { 'A' << 16 | 'P' << 8 | 'R', 3 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
706 { 'S' << 16 | 'E' << 8 | 'P', 8 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
707 { 'M' << 16 | 'A' << 8 | 'Y', 4 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
708 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
709 { 'M' << 16 | 'A' << 8 | 'R', 2 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
710 { 'F' << 16 | 'E' << 8 | 'B', 1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
711 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
712 { 'D' << 16 | 'E' << 8 | 'C', 11 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
713 { 'O' << 16 | 'C' << 8 | 'T', 9 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
714 { 'J' << 16 | 'U' << 8 | 'N', 5 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
715 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
716 { 'A' << 16 | 'U' << 8 | 'G', 7 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
717 { 'J' << 16 | 'A' << 8 | 'N', 0 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
718 { 'J' << 16 | 'U' << 8 | 'L', 6 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
719 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
720 { 'N' << 16 | 'O' << 8 | 'V', 10 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
721 };
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
722
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
723 static inline int _mstr2num(const char *s)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
724 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
725 const unsigned char *mstr = (const unsigned char *) s;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
726
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
727 /*
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
728 * We compute ucmstr (an uppercase 3 character month string stored in a
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
729 * machine word) and hash (a perfect hash based on the last 2 characters
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
730 * of the 3 character uppercase month string) from the input string s.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
731 * Note that each character from the input string is masked by 0xdf; in
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
732 * ASCII, this has the effect of converting alphabetic characters to
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
733 * uppercase while 1. not changing any nonalphabetic characters into
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
734 * alphabetic characters and 2. leaving any nul characters unchanged.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
735 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
736 * The hash value is used as an index into the MSTR2NUM_HT[] hash table.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
737 * If the ucmstr at that index matches our computed ucmstr, the mnum at
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
738 * that index is the 0-based month number corresponding to the input
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
739 * string.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
740 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
741 * Note that we never read past the end of the input string and always
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
742 * return -1 if the input string doesn't begin with a valid 3 character
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
743 * month string.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
744 */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
745
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
746 unsigned char ucmstr0 = mstr[0] & 0xdf;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
747 unsigned ucmstr = ucmstr0 << 16;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
748 if (ucmstr0 != '\0') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
749 unsigned char ucmstr1 = mstr[1] & 0xdf;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
750 ucmstr |= ucmstr1 << 8;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
751 if (ucmstr1 != '\0') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
752 unsigned char ucmstr2 = mstr[2] & 0xdf;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
753 ucmstr |= ucmstr2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
754
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
755 unsigned hash = (ucmstr1 >> 2) ^ (ucmstr2 << 1);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
756
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
757 int i = hash & MSTR2NUM_HT_MASK;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
758
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
759 if (MSTR2NUM_HT[i].ucmstr == ucmstr)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
760 return MSTR2NUM_HT[i].mnum;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
761 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
762 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
763
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
764 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
765 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
766
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
767 NSAPI_PUBLIC int util_mstr2num(const char *s)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
768 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
769 return _mstr2num(s);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
770 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
771
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
772
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
773 /* ------------------------- util_str_time_equal -------------------------- */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
774
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
775 /*
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
776 * Function to compare if two time strings are equal
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
777 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
778 * Acceptable date formats:
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
779 * Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
780 * Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
781 * Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
782 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
783 * Return 0 if equal, -1 if not equal.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
784 */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
785
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
786 static inline const char * _parse_day_month(const char *p, int *day, int *month)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
787 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
788 *day = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
789
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
790 if (*p == ',') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
791 // Parse day and month: ", 06 Nov", ", 06-Nov"
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
792 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
793 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
794 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
795 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
796 *day = *day * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
797 if (*p == ' ' || *p == '-')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
798 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
799 *month = _mstr2num(p);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
800 if (*month != -1)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
801 p += 3;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
802 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
803 // Parse month and day: " Nov 6"
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
804 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
805 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
806 *month = _mstr2num(p);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
807 if (*month != -1)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
808 p += 3;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
809 while (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
810 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
811 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
812 *day = *day * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
813 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
814
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
815 return p;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
816 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
817
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
818 static inline void _parse_year_time(const char *p, int *year, const char ** time)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
819 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
820 int _year = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
821
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
822 if (*p == '-') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
823 // Parse year and time: "-94 08:49:37"
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
824 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
825 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
826 _year = _year * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
827 if (_year < 70) {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
828 _year += 2000;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
829 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
830 _year += 1900;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
831 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
832 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
833 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
834 *time = p;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
835 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
836 // Parse year and time or time and year
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
837 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
838 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
839 if (p[0] && p[1] && p[2] == ':') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
840 // Parse time and year: "08:49:37 1994"
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
841 *time = p;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
842 p += 3;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
843 while (*p && *p != ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
844 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
845 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
846 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
847 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
848 _year = _year * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
849 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
850 // Parse year and time: "1994 08:49:37"
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
851 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
852 _year = _year * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
853 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
854 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
855 *time = p;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
856 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
857 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
858
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
859 *year = _year;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
860 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
861
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
862 NSAPI_PUBLIC int util_str_time_equal(const char *t1, const char *t2)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
863 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
864 // Skip leading whitespace and day of week
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
865 while (isspace(*t1))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
866 t1++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
867 while (isalpha(*t1))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
868 t1++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
869 while (isspace(*t2))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
870 t2++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
871 while (isalpha(*t2))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
872 t2++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
873
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
874 // Day and month: ", 06 Nov", ", 06-Nov", or " Nov 6"
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
875 int day1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
876 int month1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
877 t1 = _parse_day_month(t1, &day1, &month1);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
878 int day2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
879 int month2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
880 t2 = _parse_day_month(t2, &day2, &month2);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
881 if (day1 != day2)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
882 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
883 if (month1 != month2)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
884 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
885
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
886 // Year and time: " 1994 08:49:37", "-94 08:49:37", or " 08:49:37 1994"
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
887 int year1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
888 const char *time1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
889 _parse_year_time(t1, &year1, &time1);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
890 int year2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
891 const char *time2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
892 _parse_year_time(t2, &year2, &time2);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
893 if (year1 != year2)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
894 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
895 while (*time1 && *time1 != ' ' && *time1 == *time2) {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
896 time1++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
897 time2++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
898 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
899 if (*time2 && *time2 != ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
900 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
901
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
902 return 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
903 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
904
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
905
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
906 /* --------------------------- util_later_than ---------------------------- */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
907
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
908 static int _time_compare(const struct tm *lms, const char *ims)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
909 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
910 while (isspace(*ims))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
911 ims++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
912 while (isalpha(*ims))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
913 ims++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
914
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
915 int day;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
916 int month;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
917 ims = _parse_day_month(ims, &day, &month);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
918 if (month == -1)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
919 return 1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
920
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
921 int year;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
922 const char *time;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
923 _parse_year_time(ims, &year, &time);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
924
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
925 int rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
926
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
927 rv = (lms->tm_year + 1900) - year;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
928 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
929 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
930
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
931 rv = lms->tm_mon - month;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
932 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
933 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
934
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
935 rv = lms->tm_mday - day;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
936 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
937 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
938
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
939 const char *p = time;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
940
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
941 int hour = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
942 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
943 hour = hour * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
944 if (*p == ':')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
945 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
946
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
947 rv = lms->tm_hour - hour;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
948 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
949 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
950
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
951 int minutes = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
952 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
953 minutes = minutes * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
954 if (*p == ':')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
955 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
956
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
957 rv = lms->tm_min - minutes;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
958 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
959 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
960
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
961 int seconds = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
962 while (*p >= '0' && *p <= '9')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
963 seconds = seconds * 10 + (*p++ - '0');
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
964 if (*p == ':')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
965 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
966
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
967 rv = lms->tm_sec - seconds;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
968 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
969 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
970
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
971 return 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
972 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
973
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
974 NSAPI_PUBLIC int util_later_than(const struct tm *lms, const char *ims)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
975 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
976 /*
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
977 * Returns 0 if lms later than ims
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
978 * 0 if ims is malformed
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
979 * 1 if ims later than lms
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
980 * 1 if equal
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
981 */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
982
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
983 return _time_compare(lms, ims) <= 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
984 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
985
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
986 NSAPI_PUBLIC int util_time_equal(const struct tm *lms, const char *ims)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
987 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
988 return _time_compare(lms, ims) == 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
989 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
990
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
991
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
992 NSAPI_PUBLIC struct tm *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
993 util_gmtime(const time_t *clock, struct tm *res)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
994 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
995 return gmtime_r(clock, res);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
996 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
997
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
998 int util_isdate(const char *str) {
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
999 cxstring datestr = cx_str(str);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
1000 cxstring example = cx_str("Sun, 06 Nov 1994 08:49:37 GMT");
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1001
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1002 if(datestr.length != example.length) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1003 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1004 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1005
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1006 for(int i=0;i<datestr.length;i++) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1007 char e = example.ptr[i];
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1008 if(isdigit(e)) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1009 if(!isdigit(datestr.ptr[i])) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1010 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1011 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1012 } else if(e == ' ') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1013 if(datestr.ptr[i] != ' ') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1014 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1015 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1016 } else if(e == ',') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1017 if(datestr.ptr[i] != ',') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1018 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1019 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1020 } else if(e == ':') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1021 if(datestr.ptr[i] != ':') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1022 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1023 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1024 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1025 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1026
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 382
diff changeset
1027 if(!cx_strsuffix(datestr, cx_str("GMT"))) {
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1028 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1029 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1030
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1031 return 1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1032 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1033
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1034
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1035 /* ------------------------- util_mime_separator -------------------------- */
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1036
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1037
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1038 NSAPI_PUBLIC int util_mime_separator(char *sep)
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1039 {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1040 int size = 35; // documented in nsapi.h
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1041 int pos = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1042
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1043 sep[pos++] = CR;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1044 sep[pos++] = LF;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1045 sep[pos++] = '-';
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1046 sep[pos++] = '-';
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1047
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1048 int r[6];
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1049 for(int i=0;i<6;i++) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1050 r[i] = rand() % 10000;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1051 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1052 pos += snprintf(
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1053 sep+4,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1054 size-4,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1055 "X%04x%04x%04x%04x%04x%04xE",
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1056 r[0],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1057 r[1],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1058 r[2],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1059 r[3],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1060 r[4],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1061 r[5]);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1062
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1063 return pos;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
1064 }
273
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1065
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1066
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1067 /* ------------------------- util_html_escape -------------------------- */
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1068
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1069 NSAPI_PUBLIC char *util_html_escape(const char *s)
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1070 {
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1071 const char *in;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1072
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1073 int len = 0;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1074 for (in = s; *in; in++) {
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1075 switch (*in) {
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1076 case '<':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1077 len += 4; // &lt;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1078 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1079 case '>':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1080 len += 4; // &gt;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1081 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1082 case '&':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1083 len += 5; // &amp;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1084 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1085 case '"':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1086 len += 6; // &quot;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1087 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1088 case '\'':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1089 len += 6; // &apos;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1090 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1091 case '+':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1092 len += 5; // &#43;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1093 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1094 default:
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1095 len++;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1096 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1097 }
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1098 }
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1099
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1100 char *ns = (char *) MALLOC(len + 1);
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1101 if (!ns)
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1102 return ns;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1103
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1104 char *out = ns;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1105 for (in = s; *in; in++) {
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1106 switch (*in) {
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1107 case '<':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1108 *out++ = '&';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1109 *out++ = 'l';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1110 *out++ = 't';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1111 *out++ = ';';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1112 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1113 case '>':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1114 *out++ = '&';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1115 *out++ = 'g';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1116 *out++ = 't';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1117 *out++ = ';';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1118 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1119 case '&':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1120 *out++ = '&';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1121 *out++ = 'a';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1122 *out++ = 'm';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1123 *out++ = 'p';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1124 *out++ = ';';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1125 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1126 case '"':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1127 *out++ = '&';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1128 *out++ = 'q';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1129 *out++ = 'u';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1130 *out++ = 'o';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1131 *out++ = 't';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1132 *out++ = ';';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1133 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1134 case '\'':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1135 *out++ = '&';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1136 *out++ = 'a';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1137 *out++ = 'p';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1138 *out++ = 'o';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1139 *out++ = 's';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1140 *out++ = ';';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1141 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1142 case '+':
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1143 *out++ = '&';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1144 *out++ = '#';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1145 *out++ = '4';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1146 *out++ = '3';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1147 *out++ = ';';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1148 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1149 default:
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1150 *out++ = *in;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1151 break;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1152 }
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1153 }
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1154 *out = '\0';
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1155
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1156 return ns;
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1157 }
f4eb5d125e58 add util_html_escape implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
1158

mercurial