2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "WOSimpleHTTPParser.h"
23 #include <NGObjWeb/WOResponse.h>
24 #include <NGObjWeb/WORequest.h>
28 @implementation WOSimpleHTTPParser
30 static Class NSStringClass = Nil;
31 static BOOL debugOn = NO;
32 static BOOL heavyDebugOn = NO;
33 static int fileIOBoundary = 0;
34 static int maxUploadSize = 0;
40 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
42 debugOn = [ud boolForKey:@"WOSimpleHTTPParserDebugEnabled"];
43 heavyDebugOn = [ud boolForKey:@"WOSimpleHTTPParserHeavyDebugEnabled"];
44 fileIOBoundary = [ud integerForKey:@"WOSimpleHTTPParserFileIOBoundary"];
45 maxUploadSize = [ud integerForKey:@"WOSimpleHTTPParserMaxUploadSizeInKB"];
47 if (maxUploadSize == 0)
48 maxUploadSize = 256 * 1024; /* 256MB */
49 if (fileIOBoundary == 0)
50 fileIOBoundary = 16384;
53 NSLog(@"WOSimpleHTTPParser: max-upload-size: %dKB", maxUploadSize);
54 NSLog(@"WOSimpleHTTPParser: file-IO boundary: %d", fileIOBoundary);
58 - (id)initWithStream:(id<NGStream>)_stream {
59 if (NSStringClass == Nil) NSStringClass = [NSString class];
61 if ((self = [super init])) {
62 if ((self->io = [_stream retain]) == nil) {
67 self->readBytes = (void *)
68 [(NSObject *)self->io methodForSelector:@selector(readBytes:count:)];
69 if (self->readBytes == NULL) {
70 [self warnWithFormat:@"(%s): got invalid stream object: %@",
90 [self->content release]; self->content = nil;
91 [self->lastException release]; self->lastException = nil;
92 [self->httpVersion release]; self->httpVersion = nil;
93 [self->headers removeAllObjects];
95 if (self->lineBuffer) {
96 free(self->lineBuffer);
97 self->lineBuffer = NULL;
99 self->lineBufSize = 0;
102 /* low-level reading */
104 - (unsigned int)defaultLineSize {
108 - (NSException *)readNextLine {
111 if (self->lineBuffer == NULL) {
112 self->lineBufSize = [self defaultLineSize];
113 self->lineBuffer = malloc(self->lineBufSize + 10);
116 for (i = 0; YES; i++) {
117 register unsigned rc;
120 rc = self->readBytes(self->io, @selector(readBytes:count:), &c, 1);
123 [self debugWithFormat:@"got result %u, exception: %@",
124 rc, [self->io lastException]];
126 return [self->io lastException];
129 /* check buffer capacity */
130 if ((i + 2) > self->lineBufSize) {
131 static int reallocCount = 0;
133 if (reallocCount > 1000) {
134 static BOOL didLog = NO;
137 [self warnWithFormat:@"(%s): reallocated the HTTP line buffer %i times, "
138 @"consider increasing the default line buffer size!",
139 __PRETTY_FUNCTION__, reallocCount];
143 if (self->lineBufSize > (56 * 1024)) {
144 /* to avoid DOS attacks ... */
145 return [NSException exceptionWithName:@"HTTPParserHeaderSizeExceeded"
147 @"got a HTTP line of 100KB+ (DoS attack?)!"
151 self->lineBufSize *= 2;
152 self->lineBuffer = realloc(self->lineBuffer, self->lineBufSize + 10);
159 else if (c == '\r') {
166 self->lineBuffer[i] = c;
169 self->lineBuffer[i] = 0; /* 0-terminate buffer */
171 return nil /* nil means: everything OK */;
174 /* common HTTP parsing */
176 static NSString *ContentLengthHeaderName = @"content-length";
178 static NSString *stringForHeaderName(char *p) { /* Note: arg is _not_ const */
182 we try to be smart to avoid creation of NSString objects ...
184 register unsigned len;
187 if ((len = strlen(p)) == 0)
196 if (strcasecmp(p, "te") == 0) return @"te";
197 if (strcasecmp(p, "if") == 0) return @"if";
200 if (strcasecmp(p, "via") == 0) return @"via";
201 if (strcasecmp(p, "age") == 0) return @"age";
202 if (strcasecmp(p, "p3p") == 0) return @"p3p";
207 if (strcasecmp(p, "date") == 0) return @"date";
210 if (strcasecmp(p, "etag") == 0) return @"etag";
213 if (strcasecmp(p, "from") == 0) return @"from";
216 if (strcasecmp(p, "host") == 0) return @"host";
219 if (strcasecmp(p, "vary") == 0) return @"vary";
224 if (strcasecmp(p, "allow") == 0) return @"allow";
225 if (strcasecmp(p, "brief") == 0) return @"brief";
226 if (strcasecmp(p, "range") == 0) return @"range";
227 if (strcasecmp(p, "depth") == 0) return @"depth";
228 if (strcasecmp(p, "ua-os") == 0) return @"ua-os"; /* Entourage */
233 if (strcasecmp(p, "accept") == 0) return @"accept";
236 if (strcasecmp(p, "cookie") == 0) return @"cookie";
239 if (strcasecmp(p, "expect") == 0) return @"expect";
242 if (strcasecmp(p, "pragma") == 0) return @"pragma";
245 if (strcasecmp(p, "server") == 0) return @"server";
248 if (strcasecmp(p, "ua-cpu") == 0) return @"ua-cpu"; /* Entourage */
258 if (strcasecmp(p, "accept-charset") == 0) return @"accept-charset";
259 if (strcasecmp(p, "accept-encoding") == 0) return @"accept-encoding";
260 if (strcasecmp(p, "accept-language") == 0) return @"accept-language";
261 if (strcasecmp(p, "accept-ranges") == 0) return @"accept-ranges";
263 else if (strcasecmp(p, "authorization") == 0)
264 return @"authorization";
271 if (strcasecmp(p, "content-length") == 0)
272 return ContentLengthHeaderName;
274 if (strcasecmp(p, "content-type") == 0) return @"content-type";
275 if (strcasecmp(p, "content-md5") == 0) return @"content-md5";
276 if (strcasecmp(p, "content-range") == 0) return @"content-range";
278 if (strcasecmp(p, "content-encoding") == 0)
279 return @"content-encoding";
280 if (strcasecmp(p, "content-language") == 0)
281 return @"content-language";
283 if (strcasecmp(p, "content-location") == 0)
284 return @"content-location";
285 if (strcasecmp(p, "content-class") == 0) /* Entourage */
286 return @"content-class";
288 else if (strcasecmp(p, "call-back") == 0)
292 if (strcasecmp(p, "connection") == 0) return @"connection";
293 if (strcasecmp(p, "cache-control") == 0) return @"cache-control";
298 if (strcasecmp(p, "destination") == 0) return @"destination";
299 if (strcasecmp(p, "destroy") == 0) return @"destroy";
303 if (strcasecmp(p, "expires") == 0) return @"expires";
304 if (strcasecmp(p, "extension") == 0) return @"extension"; /* Entourage */
308 if (strcasecmp(p, "if-modified-since") == 0)
309 return @"if-modified-since";
310 if (strcasecmp(p, "if-none-match") == 0) /* Entourage */
311 return @"if-none-match";
312 if (strcasecmp(p, "if-match") == 0)
317 if (strcasecmp(p, "keep-alive") == 0) return @"keep-alive";
321 if (strcasecmp(p, "last-modified") == 0) return @"last-modified";
322 if (strcasecmp(p, "location") == 0) return @"location";
323 if (strcasecmp(p, "lock-token") == 0) return @"lock-token";
327 if (strcasecmp(p, "ms-webstorage") == 0) return @"ms-webstorage";
328 if (strcasecmp(p, "max-forwards") == 0) return @"max-forwards";
334 if (strcasecmp(p, "notification-delay") == 0)
335 return @"notification-delay";
336 if (strcasecmp(p, "notification-type") == 0)
337 return @"notification-type";
344 if (strcasecmp(p, "overwrite") == 0)
351 if (strcasecmp(p, "proxy-connection") == 0)
352 return @"proxy-connection";
358 if (strcasecmp(p, "referer") == 0) return @"referer";
365 if (strcasecmp(p, "subscription-lifetime") == 0)
366 return @"subscription-lifetime";
369 if (strcasecmp(p, "subscription-id") == 0)
370 return @"subscription-id";
373 if (strcasecmp(p, "set-cookie") == 0)
374 return @"set-cookie";
380 if (strcasecmp(p, "transfer-encoding") == 0) return @"transfer-encoding";
381 if (strcasecmp(p, "translate") == 0) return @"translate";
382 if (strcasecmp(p, "trailer") == 0) return @"trailer";
383 if (strcasecmp(p, "timeout") == 0) return @"timeout";
387 if (strcasecmp(p, "user-agent") == 0) return @"user-agent";
391 if (strcasecmp(p, "www-authenticate") == 0) return @"www-authenticate";
392 if (strcasecmp(p, "warning") == 0) return @"warning";
396 if ((p[2] == 'w') && (len > 22)) {
397 if (strstr(p, "x-webobjects-") == (void *)p) {
398 p += 13; /* skip x-webobjects- */
399 if (strcmp(p, "server-protocol") == 0)
400 return @"x-webobjects-server-protocol";
401 else if (strcmp(p, "server-protocol") == 0)
402 return @"x-webobjects-server-protocol";
403 else if (strcmp(p, "remote-addr") == 0)
404 return @"x-webobjects-remote-addr";
405 else if (strcmp(p, "remote-host") == 0)
406 return @"x-webobjects-remote-host";
407 else if (strcmp(p, "server-name") == 0)
408 return @"x-webobjects-server-name";
409 else if (strcmp(p, "server-port") == 0)
410 return @"x-webobjects-server-port";
411 else if (strcmp(p, "server-url") == 0)
412 return @"x-webobjects-server-url";
416 if (strcasecmp(p, "x-cache") == 0)
419 else if (len == 12) {
420 if (strcasecmp(p, "x-powered-by") == 0)
421 return @"x-powered-by";
423 if (strcasecmp(p, "x-zidestore-name") == 0)
424 return @"x-zidestore-name";
425 if (strcasecmp(p, "x-forwarded-for") == 0)
426 return @"x-forwarded-for";
427 if (strcasecmp(p, "x-forwarded-host") == 0)
428 return @"x-forwarded-host";
429 if (strcasecmp(p, "x-forwarded-server") == 0)
430 return @"x-forwarded-server";
436 NSLog(@"making custom header name '%s'!", p);
438 /* make name lowercase (we own the buffer, so we can work on it) */
442 for (t = (unsigned char *)p; *t != '\0'; t++)
445 return [[NSString alloc] initWithCString:p];
448 - (NSException *)parseHeader {
449 NSException *e = nil;
451 while ((e = [self readNextLine]) == nil) {
452 unsigned char *p, *v;
454 NSString *headerName;
455 NSString *headerValue;
458 printf("read header line: '%s'\n", self->lineBuffer);
460 if (strlen((char *)self->lineBuffer) == 0) {
461 /* found end of header */
465 p = self->lineBuffer;
467 if (*p == ' ' || *p == '\t') {
468 // TODO: implement folding (remember last header-key, add string)
469 [self errorWithFormat:
470 @"(%s): got a folded HTTP header line, cannot process!",
471 __PRETTY_FUNCTION__];
475 /* find key/value separator */
476 if ((v = (unsigned char *)index((char *)p, ':')) == NULL) {
477 [self warnWithFormat:@"got malformed header line: '%s'",
482 *v = '\0'; v++; /* now 'p' points to name and 'v' to value */
484 /* skip leading spaces */
485 while (*v != '\0' && (*v == ' ' || *v == '\t'))
489 /* trim trailing spaces */
490 for (idx = strlen((char *)v) - 1; idx >= 0; idx--) {
491 if ((v[idx] != ' ' && v[idx] != '\t'))
498 headerName = stringForHeaderName((char *)p);
499 headerValue = [[NSStringClass alloc] initWithCString:(char *)v];
501 if (headerName == ContentLengthHeaderName)
502 self->clen = atoi((char *)v);
504 if (headerName != nil || headerValue != nil) {
505 if (self->headers == nil)
506 self->headers = [[NSMutableDictionary alloc] initWithCapacity:32];
508 [self->headers setObject:headerValue forKey:headerName];
511 [headerValue release];
512 [headerName release];
518 - (NSException *)parseEntityOfMethod:(NSString *)_method {
520 TODO: several cases are caught:
521 a) content-length = 0 => empty data
522 b) content-length small => read into memory
523 c) content-length large => streamed into the filesystem to safe RAM
524 d) content-length unknown => ??
527 if (self->clen == 0) {
530 else if (self->clen < 0) {
531 /* I think HTTP/1.1 requires a content-length header to be present ? */
533 if ([self->httpVersion isEqualToString:@"HTTP/1.0"] ||
534 [self->httpVersion isEqualToString:@"HTTP/0.9"]) {
535 /* content-length unknown, read till EOF */
536 BOOL readToEOF = YES;
538 if ([_method isEqualToString:@"HEAD"])
540 else if ([_method isEqualToString:@"GET"])
542 else if ([_method isEqualToString:@"DELETE"])
546 [self warnWithFormat:
547 @"not processing entity of request without contentlen!"];
551 else if (self->clen > maxUploadSize*1024) {
552 /* entity is too large */
555 s = [NSString stringWithFormat:@"The maximum HTTP transaction size was "
556 @"exceeded (%d vs %d)", self->clen, maxUploadSize * 1024];
557 return [NSException exceptionWithName:@"LimitException"
558 reason:s userInfo:nil];
560 else if (self->clen > fileIOBoundary) {
561 /* we are streaming the content to a file and use a memory mapped data */
569 [self debugWithFormat:@"streaming %i bytes into file ...", self->clen];
571 fn = [[NSProcessInfo processInfo] temporaryFileName];
573 if ((t = fopen([fn cString], "w")) == NULL) {
574 [self errorWithFormat:@"could not open temporary file '%@'!", fn];
576 /* read into memory as a fallback ... */
579 [[(NGStream *)self->io safeReadDataOfLength:self->clen] retain];
580 if (self->content == nil)
581 return [self->io lastException];
585 for (toGo = self->clen; toGo > 0; ) {
586 unsigned readCount, writeCount;
588 /* read from socket */
589 readCount = [self->io readBytes:buf count:sizeof(buf)];
590 if (readCount == NGStreamError) {
598 if ((writeCount = fwrite(buf, readCount, 1, t)) != 1) {
601 writeError = ferror(t);
608 unlink([fn cString]); /* delete temporary file */
610 if (writeError == 0) {
611 return [NSException exceptionWithName:@"SystemWriteError"
612 reason:@"failed to write data to upload file"
616 return [self->io lastException];
619 self->content = [[NSData alloc] initWithContentsOfMappedFile:fn];
620 unlink([fn cString]); /* if the mmap disappears, the storage is freed */
623 /* content-length known and small */
624 //[self logWithFormat:@"reading %i bytes of the entity", self->clen];
627 [[(NGStream *)self->io safeReadDataOfLength:self->clen] retain];
628 if (self->content == nil)
629 return [self->io lastException];
631 //[self logWithFormat:@"read %i bytes.", [self->content length]];
637 /* handling expectations */
639 - (BOOL)processContinueExpectation {
640 // TODO: this should check the credentials of a request before accepting the
641 // body. The current implementation is far from optimal and only added
642 // for Mono compatibility (and actually produces the same behaviour
643 // like with HTTP/1.0 ...)
644 static char *contStatLine =
645 "HTTP/1.0 100 Continue\r\n"
646 "content-length: 0\r\n"
648 static char *failStatLine =
649 "HTTP/1.0 417 Expectation Failed\r\n"
650 "content-length: 0\r\n"
652 char *respline = NULL;
655 [self debugWithFormat:@"process 100 continue on IO: %@", self->io];
657 if (self->clen > 0 && (self->clen > (maxUploadSize * 1024))) {
658 // TODO: return a 417 expectation failed
660 respline = failStatLine;
664 respline = contStatLine;
667 if (![self->io safeWriteBytes:respline count:strlen(respline)]) {
668 ASSIGN(self->lastException, [self->io lastException]);
671 if (![self->io flush]) {
672 ASSIGN(self->lastException, [self->io lastException]);
681 - (WORequest *)parseRequest {
682 NSException *e = nil;
684 NSString *uri = @"/";
685 NSString *method = @"GET";
690 [self logWithFormat:@"HeavyDebug: parsing response ..."];
692 /* process request line */
694 if ((e = [self readNextLine])) {
695 ASSIGN(self->lastException, e);
699 printf("read request line: '%s'\n", self->lineBuffer);
702 /* sample line: "GET / HTTP/1.0" */
707 p = (char *)self->lineBuffer;
708 if ((t = index(p, ' ')) == NULL) {
709 [self logWithFormat:@"got broken request line '%s'", self->lineBuffer];
715 /* intended fall-throughs ! */
717 if (strcasecmp(p, "BPROPFIND") == 0) { method = @"BPROPFIND"; break; }
718 if (strcasecmp(p, "BPROPPATCH") == 0) { method = @"BPROPPATCH"; break; }
720 if (strcasecmp(p, "COPY") == 0) { method = @"COPY"; break; }
721 if (strcasecmp(p, "CHECKOUT") == 0) { method = @"CHECKOUT"; break; }
722 if (strcasecmp(p, "CHECKIN") == 0) { method = @"CHECKIN"; break; }
724 if (strcasecmp(p, "DELETE") == 0) { method = @"DELETE"; break; }
726 if (strcasecmp(p, "HEAD") == 0) { method = @"HEAD"; break; }
728 if (strcasecmp(p, "LOCK") == 0) { method = @"LOCK"; break; }
730 if (strcasecmp(p, "GET") == 0) { method = @"GET"; break; }
732 if (strcasecmp(p, "MKCOL") == 0) { method = @"MKCOL"; break; }
733 if (strcasecmp(p, "MOVE") == 0) { method = @"MOVE"; break; }
735 if (strcasecmp(p, "NOTIFY") == 0) { method = @"NOTIFY"; break; }
737 if (strcasecmp(p, "OPTIONS") == 0) { method = @"OPTIONS"; break; }
739 if (strcasecmp(p, "PUT") == 0) { method = @"PUT"; break; }
740 if (strcasecmp(p, "POST") == 0) { method = @"POST"; break; }
741 if (strcasecmp(p, "PROPFIND") == 0) { method = @"PROPFIND"; break; }
742 if (strcasecmp(p, "PROPPATCH") == 0) { method = @"PROPPATCH"; break; }
743 if (strcasecmp(p, "POLL") == 0) { method = @"POLL"; break; }
745 if (strcasecmp(p, "REPORT") == 0) { method = @"REPORT"; break; }
747 if (strcasecmp(p, "SEARCH") == 0) { method = @"SEARCH"; break; }
748 if (strcasecmp(p, "SUBSCRIBE") == 0) { method = @"SUBSCRIBE"; break; }
750 if (strcasecmp(p, "UNLOCK") == 0) { method = @"UNLOCK"; break; }
751 if (strcasecmp(p, "UNSUBSCRIBE")== 0) { method = @"UNSUBSCRIBE"; break; }
752 if (strcasecmp(p, "UNCHECKOUT") == 0) { method = @"UNCHECKOUT"; break; }
754 if (strcasecmp(p, "VERSION-CONTROL") == 0) {
755 method = @"VERSION-CONTROL";
761 [self debugWithFormat:@"making custom HTTP method name: '%s'", p];
762 method = [NSString stringWithCString:p];
768 p = t + 1; /* skip space */
769 while (*p != '\0' && (*p == ' ' || *p == '\t')) /* skip spaces */
773 [self logWithFormat:@"got broken request line '%s'", self->lineBuffer];
777 if ((t = index(p, ' ')) == NULL) {
778 /* the URI isn't followed by a HTTP version */
779 self->httpVersion = @"HTTP/0.9";
780 /* TODO: strip trailing spaces for better compliance */
781 uri = [NSString stringWithCString:p];
785 uri = [NSString stringWithCString:p];
789 p = t + 1; /* skip space */
790 while (*p != '\0' && (*p == ' ' || *p == '\t')) /* skip spaces */
794 self->httpVersion = @"HTTP/0.9";
795 else if (strcasecmp(p, "http/1.0") == 0)
796 self->httpVersion = @"HTTP/1.0";
797 else if (strcasecmp(p, "http/1.1") == 0)
798 self->httpVersion = @"HTTP/1.1";
800 /* TODO: strip trailing spaces */
801 self->httpVersion = [[NSString alloc] initWithCString:p];
808 if ((e = [self parseHeader])) {
809 ASSIGN(self->lastException, e);
813 [self logWithFormat:@"parsed header: %@", self->headers];
815 /* check for expectations */
817 if ((expect = [self->headers objectForKey:@"expect"])) {
818 if ([expect rangeOfString:@"100-continue"
819 options:NSCaseInsensitiveSearch].length > 0) {
820 if (![self processContinueExpectation])
828 if ((e = [self parseEntityOfMethod:method])) {
829 ASSIGN(self->lastException, e);
835 [self logWithFormat:@"HeavyDebug: got all .."];
837 r = [[WORequest alloc] initWithMethod:method
839 httpVersion:self->httpVersion
840 headers:self->headers
841 content:self->content
846 [self logWithFormat:@"HeavyDebug: request: %@", r];
848 return [r autorelease];
851 - (WOResponse *)parseResponse {
852 NSException *e = nil;
858 [self logWithFormat:@"HeavyDebug: parsing response ..."];
860 /* process response line */
862 if ((e = [self readNextLine])) {
863 ASSIGN(self->lastException, e);
867 printf("read response line: '%s'\n", self->lineBuffer);
870 /* sample line: "HTTP/1.0 200 OK" */
875 p = (char *)self->lineBuffer;
876 if ((t = index(p, ' ')) == NULL) {
877 [self logWithFormat:@"got broken response line '%s'", self->lineBuffer];
882 if (strcasecmp(p, "http/1.0") == 0)
883 self->httpVersion = @"HTTP/1.0";
884 else if (strcasecmp(p, "http/1.1") == 0)
885 self->httpVersion = @"HTTP/1.1";
887 self->httpVersion = [[NSString alloc] initWithCString:p];
891 p = t + 1; /* skip space */
892 while (*p != '\0' && (*p == ' ' || *p == '\t')) /* skip spaces */
895 [self logWithFormat:@"got broken response line '%s'", self->lineBuffer];
900 /* we don't need to parse a reason ... */
905 if ((e = [self parseHeader])) {
906 ASSIGN(self->lastException, e);
910 [self logWithFormat:@"parsed header: %@", self->headers];
915 if ((e = [self parseEntityOfMethod:nil /* parsing a response */])) {
916 ASSIGN(self->lastException, e);
922 [self logWithFormat:@"HeavyDebug: got all .."];
924 r = [[[WOResponse alloc] init] autorelease];
926 [r setHTTPVersion:self->httpVersion];
927 [r setHeaders:self->headers];
928 [r setContent:self->content];
933 [self logWithFormat:@"HeavyDebug: response: %@", r];
938 - (NSException *)lastException {
939 return self->lastException;
944 - (BOOL)isDebuggingEnabled {
948 @end /* WOSimpleHTTPParser */