7 static void printtype(Hio *hout, HContent *type, HContent *enc);
10 * these should be done better; see the reponse codes in /lib/rfc/rfc2616 for
11 * more info on what should be included.
13 #define UNAUTHED "You are not authorized to see this area.\n"
14 #define NOCONTENT "No acceptable type of data is available.\n"
15 #define NOENCODE "No acceptable encoding of the contents is available.\n"
16 #define UNMATCHED "The entity requested does not match the existing entity.\n"
17 #define BADRANGE "No bytes are avaible for the range you requested.\n"
20 * fd references a file which has been authorized & checked for relocations.
21 * send back the headers & its contents.
22 * includes checks for conditional requests & ranges.
25 sendfd(HConnect *c, int fd, Dir *dir, HContent *type, HContent *enc)
31 char *boundary, etag[32];
34 int n, nw, multir, ok;
44 * figure out the type of file and send headers
51 if(type == nil && enc == nil){
52 conts = uriclass(c, c->req.uri);
55 if(type == nil && enc == nil){
56 n = read(fd, c->xferbuf, HBufSize-1);
59 conts = dataclass(c, c->xferbuf, n);
66 type = hmkcontent(c, "application", "octet-stream", nil);
68 snprint(etag, sizeof(etag), "\"%lluxv%lux\"", qid.path, qid.vers);
69 ok = checkreq(c, type, enc, mtime, etag);
76 * check for if-range requests
78 if(c->head.range == nil
79 || c->head.ifrangeetag != nil && !etagmatch(1, c->head.ifrangeetag, etag)
80 || c->head.ifrangedate != 0 && c->head.ifrangedate != mtime){
82 c->head.ifrangeetag = nil;
83 c->head.ifrangedate = 0;
86 if(c->head.range != nil){
87 c->head.range = fixrange(c->head.range, length);
88 if(c->head.range == nil){
89 if(c->head.ifrangeetag == nil && c->head.ifrangedate == 0){
90 hprint(hout, "%s 416 Request range not satisfiable\r\n", hversion);
91 hprint(hout, "Date: %D\r\n", time(nil));
92 hprint(hout, "Server: Plan9\r\n");
93 hprint(hout, "Content-Range: bytes */%lld\r\n", length);
94 hprint(hout, "Content-Length: %d\r\n", STRLEN(BADRANGE));
95 hprint(hout, "Content-Type: text/html\r\n");
97 hprint(hout, "Connection: close\r\n");
99 hprint(hout, "Connection: Keep-Alive\r\n");
100 hprint(hout, "\r\n");
101 if(strcmp(c->req.meth, "HEAD") != 0)
102 hprint(hout, "%s", BADRANGE);
104 writelog(c, "Reply: 416 Request range not satisfiable\n");
108 c->head.ifrangeetag = nil;
109 c->head.ifrangedate = 0;
112 if(c->head.range == nil)
113 hprint(hout, "%s 200 OK\r\n", hversion);
115 hprint(hout, "%s 206 Partial Content\r\n", hversion);
117 hprint(hout, "Server: Plan9\r\n");
118 hprint(hout, "Date: %D\r\n", time(nil));
119 hprint(hout, "ETag: %s\r\n", etag);
122 * can't send some entity headers if partially responding
123 * to an if-range: etag request
127 hprint(hout, "Content-Length: %lld\r\n", length);
128 else if(r->next == nil){
129 hprint(hout, "Content-Range: bytes %ld-%ld/%lld\r\n", r->start, r->stop, length);
130 hprint(hout, "Content-Length: %ld\r\n", r->stop - r->start);
133 boundary = hmkmimeboundary(c);
134 hprint(hout, "Content-Type: multipart/byteranges; boundary=%s\r\n", boundary);
136 if(c->head.ifrangeetag == nil){
137 hprint(hout, "Last-Modified: %D\r\n", mtime);
139 printtype(hout, type, enc);
140 if(c->head.fresh_thresh)
141 hintprint(c, hout, c->req.uri, c->head.fresh_thresh, c->head.fresh_have);
145 hprint(hout, "Connection: close\r\n");
147 hprint(hout, "Connection: Keep-Alive\r\n");
148 hprint(hout, "\r\n");
150 if(strcmp(c->req.meth, "HEAD") == 0){
151 if(c->head.range == nil)
152 writelog(c, "Reply: 200 file 0\n");
154 writelog(c, "Reply: 206 file 0\n");
161 * send the file if it's a normal file
168 wrote = write(hout->fd, c->xferbuf, n);
169 if(n <= 0 || wrote == n){
170 while((n = read(fd, c->xferbuf, HBufSize)) > 0){
171 nw = write(hout->fd, c->xferbuf, n);
180 writelog(c, "Reply: 200 file %lld %lld\n", length, wrote);
188 * for multipart/byterange messages,
189 * it is not ok for the boundary string to appear within a message part.
190 * however, it probably doesn't matter, since there are lengths for every part.
194 for(; r != nil; r = r->next){
196 hprint(hout, "\r\n--%s\r\n", boundary);
197 printtype(hout, type, enc);
198 hprint(hout, "Content-Range: bytes %ld-%ld/%lld\r\n", r->start, r->stop, length);
199 hprint(hout, "Content-Length: %ld\r\n", r->stop - r->start);
200 hprint(hout, "\r\n");
204 if(seek(fd, r->start, 0) != r->start){
208 for(tr = r->stop - r->start + 1; tr; tr -= n){
212 if(read(fd, c->xferbuf, n) != n){
216 nw = write(hout->fd, c->xferbuf, n);
229 hprint(hout, "--%s--\r\n", boundary);
232 writelog(c, "Reply: 206 partial content %lld %lld\n", length, wrote);
234 writelog(c, "Reply: 206 partial content, early termination %lld %lld\n", length, wrote);
240 printtype(Hio *hout, HContent *type, HContent *enc)
242 hprint(hout, "Content-Type: %s/%s", type->generic, type->specific);
244 if(cistrcmp(type->generic, "text") == 0)
245 hprint(hout, ";charset=utf-8");
247 hprint(hout, "\r\n");
249 hprint(hout, "Content-Encoding: %s\r\n", enc->generic);
253 etagmatch(int strong, HETag *tags, char *e)
257 for(; tags != nil; tags = tags->next){
258 if(strong && tags->weak)
261 if(s[0] == '*' && s[1] == '\0')
272 if(*s == '\0' && *t == '"')
279 acceptcont(char *s, char *e, HContent *ok, char *which)
284 return seprint(s, e, "Your browser accepts any %s.<br>\n", which);
285 s = seprint(s, e, "Your browser accepts %s: ", which);
287 for(; ok != nil; ok = ok->next){
289 s = seprint(s, e, "%s%s/%s", sep, ok->generic, ok->specific);
291 s = seprint(s, e, "%s%s", sep, ok->generic);
294 return seprint(s, e, ".<br>\n");
298 * send back a nice error message if the content is unacceptable
299 * to get this message in ie, go to tools, internet options, advanced,
300 * and turn off Show Friendly HTTP Error Messages under the Browsing category
303 notaccept(HConnect *c, HContent *type, HContent *enc, char *which)
309 e = &c->xferbuf[HBufSize];
311 s = seprint(s, e, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
312 s = seprint(s, e, "<html>\n<title>Unacceptable %s</title>\n<body>\n", which);
313 s = seprint(s, e, "Your browser will not accept this data, %H, because of its %s.<br>\n", c->req.uri, which);
314 s = seprint(s, e, "Its Content-Type is %s/%s", type->generic, type->specific);
316 s = seprint(s, e, ", and Content-Encoding is %s", enc->generic);
317 s = seprint(s, e, ".<br>\n\n");
319 s = acceptcont(s, e, c->head.oktype, "Content-Type");
320 s = acceptcont(s, e, c->head.okencode, "Content-Encoding");
321 s = seprint(s, e, "</body>\n</html>\n");
323 hprint(hout, "%s 406 Not Acceptable\r\n", hversion);
324 hprint(hout, "Server: Plan9\r\n");
325 hprint(hout, "Date: %D\r\n", time(nil));
326 hprint(hout, "Content-Type: text/html\r\n");
327 hprint(hout, "Content-Length: %lud\r\n", s - c->xferbuf);
329 hprint(hout, "Connection: close\r\n");
331 hprint(hout, "Connection: Keep-Alive\r\n");
332 hprint(hout, "\r\n");
333 if(strcmp(c->req.meth, "HEAD") != 0)
334 hwrite(hout, c->xferbuf, s - c->xferbuf);
335 writelog(c, "Reply: 406 Not Acceptable\nReason: %s\n", which);
340 * check time and entity tag conditions.
343 checkreq(HConnect *c, HContent *type, HContent *enc, long mtime, char *etag)
349 if(c->req.vermaj >= 1 && c->req.vermin >= 1 && !hcheckcontent(type, c->head.oktype, "Content-Type", 0))
350 return notaccept(c, type, enc, "Content-Type");
351 if(c->req.vermaj >= 1 && c->req.vermin >= 1 && !hcheckcontent(enc, c->head.okencode, "Content-Encoding", 0))
352 return notaccept(c, type, enc, "Content-Encoding");
355 * can use weak match only with get or head;
356 * this always uses strong matches
358 m = etagmatch(1, c->head.ifnomatch, etag);
360 if(m && strcmp(c->req.meth, "GET") != 0 && strcmp(c->req.meth, "HEAD") != 0
361 || c->head.ifunmodsince && c->head.ifunmodsince < mtime
362 || c->head.ifmatch != nil && !etagmatch(1, c->head.ifmatch, etag)){
363 hprint(hout, "%s 412 Precondition Failed\r\n", hversion);
364 hprint(hout, "Server: Plan9\r\n");
365 hprint(hout, "Date: %D\r\n", time(nil));
366 hprint(hout, "Content-Type: text/html\r\n");
367 hprint(hout, "Content-Length: %d\r\n", STRLEN(UNMATCHED));
369 hprint(hout, "Connection: close\r\n");
371 hprint(hout, "Connection: Keep-Alive\r\n");
372 hprint(hout, "\r\n");
373 if(strcmp(c->req.meth, "HEAD") != 0)
374 hprint(hout, "%s", UNMATCHED);
375 writelog(c, "Reply: 412 Precondition Failed\n");
379 if(c->head.ifmodsince >= mtime
380 && (m || c->head.ifnomatch == nil)){
382 * can only send back Date, ETag, Content-Location,
383 * Expires, Cache-Control, and Vary entity-headers
385 hprint(hout, "%s 304 Not Modified\r\n", hversion);
386 hprint(hout, "Server: Plan9\r\n");
387 hprint(hout, "Date: %D\r\n", time(nil));
388 hprint(hout, "ETag: %s\r\n", etag);
390 hprint(hout, "Connection: close\r\n");
392 hprint(hout, "Connection: Keep-Alive\r\n");
393 hprint(hout, "\r\n");
394 writelog(c, "Reply: 304 Not Modified\n");
401 * length is the actual length of the entity requested.
402 * discard any range requests which are invalid,
403 * ie start after the end, or have stop before start.
404 * rewrite suffix requests
407 fixrange(HRange *h, long length)
415 * rewrite each range to reflect the actual length of the file
416 * toss out any invalid ranges
419 for(r = h; r != nil; r = r->next){
421 r->start = length - r->stop;
422 if(r->start >= length)
424 r->stop = length - 1;
427 if(r->stop >= length)
428 r->stop = length - 1;
429 if(r->start > r->stop){
439 * merge consecutive overlapping or abutting ranges
441 * not clear from rfc2616 how much merging needs to be done.
442 * this code merges only if a range is adjacent to a later starting,
443 * over overlapping or abutting range. this allows a client
444 * to request wanted data first, followed by other data.
445 * this may be useful then fetching part of a page, then the adjacent regions.
454 if(r->start <= rr->start && r->stop + 1 >= rr->start){
455 if(r->stop < rr->stop)