src/server/util/util.c

Tue, 10 Nov 2015 21:11:06 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 10 Nov 2015 21:11:06 +0100
changeset 110
43a746e905f6
parent 109
8a0a7754f123
child 113
57e706fed20a
permissions
-rw-r--r--

refactored IO system

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"
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
56 #include <ucx/string.h>
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
57 #include <ucx/mempool.h>
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
59 #include "pblock.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 #include "util.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
62
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
62
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
63
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
64 /* ------------------------------ _uudecode ------------------------------- */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
65
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
66 static const unsigned char pr2six[256] = {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
67 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
68 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
69 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
70 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
71 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
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,64,64,64,64,64,
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
74 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
75 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
76 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
77 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 };
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
79
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
80 /** 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
81 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
82 register char *bufin = bufcoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
83 register int nprbytes;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
84 size_t nbytesdecoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
85
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
86 /* Find the length */
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
87 nprbytes = (int) codedbytes;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
88 while(pr2six[(int)(bufin[nprbytes-1])] >= 64) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
89 nprbytes--;
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 nbytesdecoded = ((nprbytes+3)/4) * 3;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
92
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
93 while (nprbytes > 0) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
94 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
95 (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
96 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
97 (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
98 *(bufout++) = (unsigned char)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
99 (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
100 bufin += 4;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
101 nprbytes -= 4;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
102 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
103
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
104 if(nprbytes & 03) {
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
105 if(pr2six[(int)bufin[-2]] > 63)
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
106 nbytesdecoded -= 2;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
107 else
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
108 nbytesdecoded -= 1;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
109 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
110
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
111 return nbytesdecoded;
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
112 }
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
113
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
114
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
115 /* --------------------------- util_env_create ---------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
116
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
117
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
118 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
119 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
120 int x;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
121
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
122 if(!env) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
123 *pos = 0;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
124 return (char **) MALLOC((n + 1)*sizeof(char *));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
125 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
126 else {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
127 for(x = 0; (env[x]); x++);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
128 env = (char **) REALLOC(env, (n + x + 1)*(sizeof(char *)));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
129 *pos = x;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
130 return env;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
131 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
132 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
133
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
134
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
135 /* ---------------------------- util_env_free ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
136
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
137
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
138 NSAPI_PUBLIC void util_env_free(char **env)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
139 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
140 register char **ep = env;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
141
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
142 for(ep = env; *ep; ep++)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
143 FREE(*ep);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
144 FREE(env);
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
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
147 /* ----------------------------- util_env_str ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
148
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 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
151 char *t;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
152
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
153 t = (char *) MALLOC(strlen(name)+strlen(value)+2); /* 2: '=' and '\0' */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
154
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
155 sprintf(t, "%s=%s", name, value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
156
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
157 return t;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
158 }
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 /* --------------------------- util_env_replace --------------------------- */
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
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
164 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
165 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
166 int x, y, z;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
167 char *i;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
168
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
169 for(x = 0; env[x]; x++) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
170 i = strchr(env[x], '=');
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
171 *i = '\0';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
172 if(!strcmp(env[x], name)) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
173 y = strlen(env[x]);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
174 z = strlen(value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
175
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
176 env[x] = (char *) REALLOC(env[x], y + z + 2);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
177 util_sprintf(&env[x][y], "=%s", value);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
178 return;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
179 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
180 *i = '=';
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
182 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
183
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
184
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
185 /* ---------------------------- util_env_find ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
186
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 NSAPI_PUBLIC char *util_env_find(char **env, const char *name)
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 char *i;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
191 int x, r;
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 for(x = 0; env[x]; x++) {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
194 i = strchr(env[x], '=');
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
195 *i = '\0';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
196 r = !strcmp(env[x], name);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
197 *i = '=';
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
198 if(r)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
199 return i + 1;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
201 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
202 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
203
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 /* ---------------------------- util_env_copy ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
206
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
207
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
208 NSAPI_PUBLIC char **util_env_copy(char **src, char **dst)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
209 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
210 char **src_ptr;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
211 int src_cnt;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
212 int index;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
213
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
214 if (!src)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
215 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
216
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
217 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
218
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
219 if (!src_cnt)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
220 return NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
221
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
222 dst = util_env_create(dst, src_cnt, &index);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
223
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
224 for (src_ptr = src; *src_ptr; index++, src_ptr++)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
225 dst[index] = STRDUP(*src_ptr);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
226 dst[index] = NULL;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
227
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
228 return dst;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 }
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
230
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
231 /* ----------------------------- util_sprintf ----------------------------- */
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
232
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
233 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
234 va_list args)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
235 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
236 return vsnprintf(s, n, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
237 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
238
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
239 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
240 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
241 va_list args;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
242 va_start(args, fmt);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
243 return vsnprintf(s, n, fmt, args);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
244 }
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
245
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
246 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
247 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
248 return vsprintf(s, fmt, args);
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 NSAPI_PUBLIC int util_sprintf(char *s, const char *fmt, ...)
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
252 {
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
253 va_list args;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
254 va_start(args, fmt);
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
255 return vsprintf(s, fmt, args);
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
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
258 // TODO: asprintf
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
259
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
261
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
262 /* -------------------------- util_uri_unescape --------------------------- */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
263
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
264 NSAPI_PUBLIC void util_uri_unescape(char *s)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
265 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
266 char *t, *u;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
267
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
268 for(t = s, u = s; *t; ++t, ++u) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
269 if((*t == '%') && t[1] && t[2]) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
270 *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
271 (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
272 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
273 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
274 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
275 if(u != t)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
276 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
277 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
278 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
279 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
280
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
281 /*
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
282 * Same as util_uri_unescape, but returns success/failure
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
283 */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
284 NSAPI_PUBLIC int util_uri_unescape_strict(char *s)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
285 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
286 char *t, *u, t1, t2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
287 int rv = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
288
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
289 for(t = s, u = s; *t; ++t, ++u) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
290 if (*t == '%') {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
291 t1 = t[1] & 0xdf; /* [a-f] -> [A-F] */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
292 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
293 rv = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
294
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
295 t2 = t[2] & 0xdf; /* [a-f] -> [A-F] */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
296 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
297 rv = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
298
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
299 *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
300 (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
301 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
302 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
303 else if (u != t)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
304 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
305 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
306 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
307
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
308 return rv;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
309 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
310
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
311
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
312 NSAPI_PUBLIC int
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
313 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
314 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
315 const char *t = src;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
316 char *u = trg == NULL ? (char *)src : trg;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
317 int rlen = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
318
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
319 if (len == -1)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
320 len = strlen (src);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
321
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
322 for( ; len && *t; ++t, ++u, len--, rlen++)
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
323 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
324 if((*t == '%') && t[1] && t[2])
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
325 {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
326 *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
327 (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
328 t += 2;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
329 len-= 2;
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 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
332 if (*t == '+')
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
333 *u = ' ';
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
334 else
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
335 *u = *t;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
336 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
337 *u = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
338 return rlen;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
339 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
340
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
341
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 if(v[0] == 'T' || v[0] == 't') {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 return 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 if(v[0] == 'F' || v[0] == 'f') {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 return 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 return def;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
352 int util_getboolean_s(sstr_t s, int def) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
353 if(s.length == 0) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
354 return def;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
355 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
356 if(s.ptr[0] == 'T' || s.ptr[0] == 't') {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
357 return 1;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
358 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
359 if(s.ptr[0] == 'F' || s.ptr[0] == 'f') {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
360 return 0;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
361 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
362 return def;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
363 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 104
diff changeset
364
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 /* ------------------------------ util_itoa ------------------------------- */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 NSAPI_PUBLIC int util_itoa(int i, char *a)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 int len = util_i64toa(i, a);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 PR_ASSERT(len < UTIL_ITOA_SIZE);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 return len;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 NSAPI_PUBLIC int INTutil_itoa(int i, char *a) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 return INTutil_i64toa(i, a);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 /* ----------------------------- util_i64toa ------------------------------ */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 * Assumption: Reversing the digits will be faster in the general case
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 * 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
387 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388
24
1a7853a4257e removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
389 NSAPI_PUBLIC int INTutil_i64toa(int64_t i, char *a)
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 register int x, y, p;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 register char c;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 int negative;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 negative = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 if(i < 0) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 *a++ = '-';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 negative = 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 i = -i;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 p = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 while(i > 9) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 a[p++] = (i%10) + '0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 i /= 10;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 a[p++] = i + '0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 if(p > 1) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 for(x = 0, y = p - 1; x < y; ++x, --y) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 c = a[x];
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 a[x] = a[y];
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 a[y] = c;
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 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 a[p] = '\0';
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 //PR_ASSERT(p + negative < UTIL_I64TOA_SIZE);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 return p + negative;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 }
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
421
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
422
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
423
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
424 #ifndef XP_WIN32
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
425 NSAPI_PUBLIC struct passwd *
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
426 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
427 int buflen)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
428 {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
429 struct passwd *rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
430
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
431 #if defined(AIX) || defined(LINUX) || defined(HPUX) || defined(BSD)
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
432 errno = getpwnam_r(name, result, buffer, buflen, &rv);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
433 if (errno != 0)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
434 rv = NULL;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
435 #else
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
436 rv = getpwnam_r(name, result, buffer, buflen);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
437 #endif
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
438
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
439 return rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
440 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
441 #endif
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
442
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
443
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
444 #ifndef XP_WIN32
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
445 NSAPI_PUBLIC struct passwd *
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
446 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
447 {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
448 struct passwd *rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
449
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
450 #if defined(AIX) || defined(LINUX) || defined(HPUX) || defined(BSD)
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
451 errno = getpwuid_r(uid, result, buffer, buflen, &rv);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
452 if (errno != 0)
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
453 rv = NULL;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
454 #else
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
455 rv = getpwuid_r(uid, result, buffer, buflen);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
456 #endif
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
457
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
458 return rv;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
459 }
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 24
diff changeset
460 #endif
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
461
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
462
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
463 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
464 switch(errno_value) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
465 case 0: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
466 return 200;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
467 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
468 case EACCES: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
469 return 403;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
470 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
471 case ENOENT: {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
472 return 404;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
473 break;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
474 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
475 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
476 return 500;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
477 }
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
478
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
479
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
480
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
481 NSAPI_PUBLIC
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
482 sstr_t util_path_append(pool_handle_t *pool, char *path, char *ch) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
483 sstr_t parent = sstr(path);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
484 sstr_t child = sstr(ch);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
485 sstr_t newstr;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
486 sstr_t s;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
487
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
488
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
489 UcxAllocator a = util_pool_allocator(pool);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
490 if(s.length == 1) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
491 newstr = sstrcat_a(&a, 3, parent, s, child);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
492 } else {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
493 newstr = sstrcat_a(&a, 2, parent, child);
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
494 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
495
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
496 return newstr;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
497 }
63
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
498
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
499 sstr_t util_path_remove_last(sstr_t path) {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
500 int i;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
501 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
502 char c = path.ptr[i];
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
503 if(c == '/') {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
504 path.ptr[i] = 0;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
505 path.length = i;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
506 break;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
507 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
508 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
509 if(i < 0) {
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
510 path.ptr = NULL;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
511 path.length = 0;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
512 }
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
513 return path;
66442f81f823 supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
514 }
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
515
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
516 void util_add_ppath(sstr_t root, sstr_t path, pblock *vars) {
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
517 // concat path
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
518 size_t length = root.length + path.length;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
519 char *translated_path = alloca(length);
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
520
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
521 memcpy(translated_path, root.ptr, root.length);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
522 memcpy(translated_path + root.length, path.ptr, path.length);
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
523
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
524
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
525 // add path to specified pblock
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
526 pblock_kvinsert(
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
527 pb_key_ppath,
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
528 translated_path,
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
529 length,
96
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
530 vars);
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
531 }
0185b13bf41f added document-root and pfx2dir nametrans safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
532
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
533
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
534 UcxAllocator util_pool_allocator(pool_handle_t *pool) {
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
535 UcxAllocator a;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
536 a.malloc = (ucx_allocator_malloc)pool_malloc;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
537 a.calloc = (ucx_allocator_calloc)pool_calloc;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
538 a.realloc = (ucx_allocator_realloc)pool_realloc;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
539 a.free = (ucx_allocator_free)pool_free;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
540 a.pool = pool;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
541 return a;
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
542 }
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
543
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
544
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
545 // new - code from params.cpp
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
546 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
547 pblock *pb = pblock_create_pool(pool, 32);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
548 if(!pb) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
549 return NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
550 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
551 if(!query || !(*query)) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
552 return pb;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
553 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
554
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
555 int loopFlag = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
556 int nl = 0; // size of the name substring
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
557 int vl = 0; // size of the value substring
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
558 int state = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
559 const char *np = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
560 const char *vp = NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
561
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
562 while (loopFlag) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
563 char delim = *query++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
564 switch (delim) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
565 case '&':
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
566 case '\0': {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
567 if(!delim) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
568 loopFlag = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
569 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
570
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
571 state = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
572
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
573 if(nl > 0) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
574 util_uri_unescape_plus(np, NULL, nl);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
575 util_uri_unescape_plus(vp, NULL, vl);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
576 pblock_nvlinsert(np, nl, vp, vl, pb);
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
577 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
578
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
579 nl = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
580 vl = 0;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
581 vp = NULL;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
582 np = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
583 break;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
584 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
585 case '=': {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
586 state = 1;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
587 vp = query;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
588 break;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
589 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
590 default: {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
591 if(state) {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
592 vl++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
593 } else {
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
594 nl++;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
595 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
596 }
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
597 } /* switch */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
598 } /* while */
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
599
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
600 return pb;
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
601 }
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
602
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
603 // TODO: remove
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
604 sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) {
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
605 sstr_t newstring;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
606 newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1);
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
607 if (newstring.ptr != NULL) {
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
608 newstring.length = s.length;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
609 newstring.ptr[newstring.length] = 0;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
610
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
611 memcpy(newstring.ptr, s.ptr, s.length);
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
612 }
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
613
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
614 return newstring;
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
615 }
102
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
616
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
617
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
618
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
619 /* ---------------------------- util_mstr2num ----------------------------- */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
620
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
621 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
622
103
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
623 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
624 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
625 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
626 };
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
627
d3b514e2ddbd added date header to response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 102
diff changeset
628 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
629 { '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
630 { '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
631 { '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
632 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
633 { '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
634 { '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
635 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
636 { '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
637 { '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
638 { '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
639 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
640 { '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
641 { '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
642 { '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
643 { 0, -1 },
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
644 { '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
645 };
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
646
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
647 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
648 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
649 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
650
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
651 /*
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
652 * 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
653 * 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
654 * 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
655 * 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
656 * 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
657 * 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
658 * 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
659 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
660 * 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
661 * 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
662 * 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
663 * string.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
664 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
665 * 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
666 * 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
667 * month string.
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
668 */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
669
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
670 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
671 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
672 if (ucmstr0 != '\0') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
673 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
674 ucmstr |= ucmstr1 << 8;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
675 if (ucmstr1 != '\0') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
676 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
677 ucmstr |= ucmstr2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
678
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
679 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
680
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
681 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
682
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
683 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
684 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
685 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
686 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
687
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
688 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
689 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
690
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
691 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
692 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
693 return _mstr2num(s);
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
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 /* ------------------------- 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
698
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
699 /*
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
700 * 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
701 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
702 * Acceptable date formats:
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
703 * 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
704 * 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
705 * 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
706 *
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
707 * 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
708 */
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
709
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
710 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
711 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
712 *day = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
713
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
714 if (*p == ',') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
715 // 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
716 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
717 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
718 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
719 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
720 *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
721 if (*p == ' ' || *p == '-')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
722 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
723 *month = _mstr2num(p);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
724 if (*month != -1)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
725 p += 3;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
726 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
727 // 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
728 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
729 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
730 *month = _mstr2num(p);
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
731 if (*month != -1)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
732 p += 3;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
733 while (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
734 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
735 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
736 *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
737 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
738
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
739 return p;
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
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
742 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
743 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
744 int _year = 0;
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 if (*p == '-') {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
747 // 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
748 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
749 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
750 _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
751 if (_year < 70) {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
752 _year += 2000;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
753 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
754 _year += 1900;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
755 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
756 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
757 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
758 *time = p;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
759 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
760 // 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
761 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
762 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
763 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
764 // 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
765 *time = p;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
766 p += 3;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
767 while (*p && *p != ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
768 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
769 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
770 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
771 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
772 _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
773 } else {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
774 // 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
775 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
776 _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
777 if (*p == ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
778 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
779 *time = p;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
780 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
781 }
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 *year = _year;
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 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
787 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
788 // 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
789 while (isspace(*t1))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
790 t1++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
791 while (isalpha(*t1))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
792 t1++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
793 while (isspace(*t2))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
794 t2++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
795 while (isalpha(*t2))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
796 t2++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
797
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
798 // 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
799 int day1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
800 int month1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
801 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
802 int day2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
803 int month2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
804 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
805 if (day1 != day2)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
806 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
807 if (month1 != month2)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
808 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
809
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
810 // 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
811 int year1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
812 const char *time1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
813 _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
814 int year2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
815 const char *time2;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
816 _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
817 if (year1 != year2)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
818 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
819 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
820 time1++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
821 time2++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
822 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
823 if (*time2 && *time2 != ' ')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
824 return -1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
825
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
826 return 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
827 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
828
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
829
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
830 /* --------------------------- util_later_than ---------------------------- */
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 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
833 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
834 while (isspace(*ims))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
835 ims++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
836 while (isalpha(*ims))
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
837 ims++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
838
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
839 int day;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
840 int month;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
841 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
842 if (month == -1)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
843 return 1;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
844
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
845 int year;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
846 const char *time;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
847 _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
848
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
849 int rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
850
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
851 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
852 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
853 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
854
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
855 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
856 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
857 return rv;
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 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
860 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
861 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
862
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
863 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
864
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
865 int hour = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
866 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
867 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
868 if (*p == ':')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
869 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
870
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
871 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
872 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
873 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
874
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
875 int minutes = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
876 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
877 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
878 if (*p == ':')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
879 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
880
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
881 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
882 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
883 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
884
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
885 int seconds = 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
886 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
887 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
888 if (*p == ':')
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
889 p++;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
890
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
891 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
892 if (rv)
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
893 return rv;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
894
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
895 return 0;
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
896 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
897
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
898 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
899 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
900 /*
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
901 * 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
902 * 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
903 * 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
904 * 1 if equal
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
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
907 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
908 }
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 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
911 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
912 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
913 }
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
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
916 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
917 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
918 {
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
919 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
920 }
136a76e293b5 added etag and conditional request implementation from Open Web Server
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
921
104
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
922 int util_isdate(char *str) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
923 sstr_t datestr = sstr(str);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
924 sstr_t example = S("Sun, 06 Nov 1994 08:49:37 GMT");
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
925
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
926 if(datestr.length != example.length) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
927 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
928 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
929
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
930 for(int i=0;i<datestr.length;i++) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
931 char e = example.ptr[i];
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
932 if(isdigit(e)) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
933 if(!isdigit(datestr.ptr[i])) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
934 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
935 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
936 } else if(e == ' ') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
937 if(datestr.ptr[i] != ' ') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
938 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
939 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
940 } else if(e == ',') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
941 if(datestr.ptr[i] != ',') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
942 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
943 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
944 } else if(e == ':') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
945 if(datestr.ptr[i] != ':') {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
946 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
947 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
948 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
949 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
950
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
951 if(!sstrsuffix(datestr, S("GMT"))) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
952 return 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
953 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
954
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
955 return 1;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
956 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
957
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
958
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
959 /* ------------------------- util_mime_separator -------------------------- */
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
960
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
961
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
962 NSAPI_PUBLIC int util_mime_separator(char *sep)
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
963 {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
964 int size = 35; // documented in nsapi.h
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
965 int pos = 0;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
966
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
967 sep[pos++] = CR;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
968 sep[pos++] = LF;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
969 sep[pos++] = '-';
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
970 sep[pos++] = '-';
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
971
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
972 int r[6];
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
973 for(int i=0;i<6;i++) {
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
974 r[i] = rand() % 10000;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
975 }
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
976 pos += snprintf(
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
977 sep+4,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
978 size-4,
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
979 "X%04x%04x%04x%04x%04x%04xE",
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
980 r[0],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
981 r[1],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
982 r[2],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
983 r[3],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
984 r[4],
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
985 r[5]);
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
986
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
987 return pos;
a8acbb12f27c implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
988 }

mercurial