|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
|
5 * |
|
6 * THE BSD LICENSE |
|
7 * |
|
8 * Redistribution and use in source and binary forms, with or without |
|
9 * modification, are permitted provided that the following conditions are met: |
|
10 * |
|
11 * Redistributions of source code must retain the above copyright notice, this |
|
12 * list of conditions and the following disclaimer. |
|
13 * Redistributions in binary form must reproduce the above copyright notice, |
|
14 * this list of conditions and the following disclaimer in the documentation |
|
15 * and/or other materials provided with the distribution. |
|
16 * |
|
17 * Neither the name of the nor the names of its contributors may be |
|
18 * used to endorse or promote products derived from this software without |
|
19 * specific prior written permission. |
|
20 * |
|
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
|
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 */ |
|
33 |
|
34 #include <stdio.h> |
|
35 #include <stdlib.h> |
|
36 |
|
37 #include "http.h" |
|
38 #include "protocol.h" |
|
39 |
|
40 #include "../util/pblock.h" |
|
41 #include "../util/pool.h" |
|
42 #include "../util/util.h" |
|
43 |
|
44 |
|
45 static int http_etag = 1; |
|
46 |
|
47 /* --------------------------- http_format_etag --------------------------- */ |
|
48 |
|
49 NSAPI_PUBLIC void http_format_etag(Session *sn, Request *rq, char *etagp, int etaglen, off_t size, time_t mtime) |
|
50 { |
|
51 /* |
|
52 * Heuristic for weak/strong validator: if the request was made within |
|
53 * a second of the last-modified date, then we send a weak Etag. |
|
54 */ |
|
55 if (rq->req_start - mtime <= 1) { |
|
56 /* Send a weak Etag */ |
|
57 *etagp++ = 'W'; |
|
58 *etagp++ = '/'; |
|
59 etaglen -= 2; |
|
60 } |
|
61 |
|
62 /* |
|
63 * Create an Etag out of the metadata (size, mtime) to obtain Etag |
|
64 * uniqueness. |
|
65 */ |
|
66 util_snprintf(etagp, etaglen, "\"%x-%x\"", (int)size, (int)mtime); |
|
67 } |
|
68 |
|
69 |
|
70 /* --------------------------- http_weaken_etag --------------------------- */ |
|
71 |
|
72 NSAPI_PUBLIC void http_weaken_etag(Session *sn, Request *rq) |
|
73 { |
|
74 /* Replace any existing strong Etag with a weak Etag */ |
|
75 pb_param *pp = pblock_findkey(pb_key_etag, rq->srvhdrs); |
|
76 if (pp) { |
|
77 if (pp->value[0] != 'W' || pp->value[1] != '/') { |
|
78 char *weak = (char *) pool_malloc(sn->pool, 2 + strlen(pp->value) + 1); |
|
79 |
|
80 weak[0] = 'W'; |
|
81 weak[1] = '/'; |
|
82 strcpy(weak + 2, pp->value); |
|
83 |
|
84 pool_free(sn->pool, pp->value); |
|
85 pp->value = weak; |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 |
|
91 /* ------------------------------ match_etag ------------------------------ */ |
|
92 |
|
93 NSAPI_PUBLIC int http_match_etag(const char *header, const char *etag, int strong) |
|
94 { |
|
95 if (!etag) |
|
96 return 0; /* mismatch */ |
|
97 |
|
98 if (header[0] == '*') |
|
99 return 1; /* match */ |
|
100 |
|
101 if (etag[0] == 'W' && etag[1] == '/') { |
|
102 /* Weak Etags never match when using the strong validator */ |
|
103 if (strong) |
|
104 return 0; /* mismatch */ |
|
105 |
|
106 etag += 2; |
|
107 } |
|
108 |
|
109 /* Look for Etag in header */ |
|
110 const char *found = strstr(header, etag); |
|
111 if (!found) |
|
112 return 0; /* mismatch */ |
|
113 |
|
114 /* Weak Etags never match when using the strong validator */ |
|
115 if (strong && found >= header + 2 && found[-2] == 'W' && found[-1] == '/') |
|
116 return 0; /* mismatch */ |
|
117 |
|
118 return 1; /* match */ |
|
119 } |
|
120 |
|
121 |
|
122 /* ----------------------- http_check_preconditions ----------------------- */ |
|
123 |
|
124 NSAPI_PUBLIC int http_check_preconditions(Session *sn, Request *rq, struct tm *mtm, const char *etag) |
|
125 { |
|
126 char *header; |
|
127 |
|
128 /* If-modified-since */ |
|
129 header = pblock_findkeyval(pb_key_if_modified_since, rq->headers); |
|
130 if (header) { |
|
131 if (mtm && util_later_than(mtm, header)) { |
|
132 protocol_status(sn, rq, PROTOCOL_NOT_MODIFIED, NULL); |
|
133 return REQ_ABORTED; |
|
134 } |
|
135 } |
|
136 |
|
137 /* If-unmodified-since */ |
|
138 header = pblock_findkeyval(pb_key_if_unmodified_since, rq->headers); |
|
139 if (header) { |
|
140 if (mtm && !util_later_than(mtm, header)) { |
|
141 //PRTime temptime; |
|
142 //PRStatus status = PR_ParseTimeString(header, PR_TRUE, &temptime); |
|
143 //if (status == PR_SUCCESS) { |
|
144 // http_status(sn, rq, PROTOCOL_PRECONDITION_FAIL, NULL); |
|
145 // return REQ_ABORTED; |
|
146 //} |
|
147 } |
|
148 } |
|
149 |
|
150 /* If-none-match */ |
|
151 header = pblock_findkeyval(pb_key_if_none_match, rq->headers); |
|
152 if (header) { |
|
153 /* If the If-none-match header matches the current Etag... */ |
|
154 if (ISMGET(rq) || ISMHEAD(rq)) { |
|
155 if (http_match_etag(header, etag, PR_FALSE)) { |
|
156 protocol_status(sn, rq, PROTOCOL_NOT_MODIFIED, NULL); |
|
157 return REQ_ABORTED; |
|
158 } |
|
159 } else { |
|
160 if (http_match_etag(header, etag, PR_TRUE)) { |
|
161 protocol_status(sn, rq, PROTOCOL_PRECONDITION_FAIL, NULL); |
|
162 return REQ_ABORTED; |
|
163 } |
|
164 } |
|
165 } |
|
166 |
|
167 /* If-match */ |
|
168 header = pblock_findkeyval(pb_key_if_match, rq->headers); |
|
169 if (header) { |
|
170 /* If the If-match header matches the current Etag... */ |
|
171 if (!http_match_etag(header, etag, PR_TRUE)) { |
|
172 protocol_status(sn, rq, PROTOCOL_PRECONDITION_FAIL, NULL); |
|
173 return REQ_ABORTED; |
|
174 } |
|
175 } |
|
176 |
|
177 return REQ_PROCEED; |
|
178 } |
|
179 |
|
180 |
|
181 /* ---------------------------- http_set_finfo ---------------------------- */ |
|
182 |
|
183 static inline int set_finfo(Session *sn, Request *rq, off_t size, time_t mtime) |
|
184 { |
|
185 struct tm mtms; |
|
186 struct tm *mtm = system_gmtime(&mtime, &mtms); |
|
187 pb_param *pp; |
|
188 |
|
189 /* Insert Last-modified */ |
|
190 if (mtm) { |
|
191 pp = pblock_key_param_create(rq->srvhdrs, pb_key_last_modified, NULL, |
|
192 HTTP_DATE_LEN); |
|
193 if (!pp || !pp->value) |
|
194 return REQ_ABORTED; |
|
195 strftime(pp->value, HTTP_DATE_LEN, HTTP_DATE_FMT, mtm); |
|
196 pblock_kpinsert(pb_key_last_modified, pp, rq->srvhdrs); |
|
197 } |
|
198 |
|
199 /* Insert Content-length */ |
|
200 const int content_length_size = 21; |
|
201 pp = pblock_key_param_create(rq->srvhdrs, pb_key_content_length, NULL, |
|
202 content_length_size); |
|
203 if (!pp || !pp->value) |
|
204 return REQ_ABORTED; |
|
205 snprintf(pp->value, content_length_size, "%lld", (long long)size); |
|
206 pblock_kpinsert(pb_key_content_length, pp, rq->srvhdrs); |
|
207 |
|
208 char *etag; |
|
209 if (http_etag) { |
|
210 /* Insert Etag */ |
|
211 pp = pblock_key_param_create(rq->srvhdrs, pb_key_etag, NULL, MAX_ETAG); |
|
212 if (!pp || !pp->value) |
|
213 return REQ_ABORTED; |
|
214 http_format_etag(sn, rq, pp->value, MAX_ETAG, size, mtime); |
|
215 pblock_kpinsert(pb_key_etag, pp, rq->srvhdrs); |
|
216 etag = pp->value; |
|
217 } else { |
|
218 etag = NULL; |
|
219 } |
|
220 |
|
221 /* Check If-modified-since, etc. */ |
|
222 return http_check_preconditions(sn, rq, mtm, etag); |
|
223 } |
|
224 |
|
225 NSAPI_PUBLIC int http_set_finfo(Session *sn, Request *rq, struct stat *finfo) |
|
226 { |
|
227 return set_finfo(sn, rq, finfo->st_size, finfo->st_mtime); |
|
228 } |
|
229 |
|
230 |