2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo 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 OGo 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 OGo; 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>
27 @implementation WOSimpleHTTPParser
29 static Class NSStringClass = Nil;
30 static BOOL debugOn = NO;
31 static BOOL heavyDebugOn = NO;
32 static int fileIOBoundary = 16384;
33 static int maxUploadSize = 256 * 1024; /* 256MB */
39 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
41 debugOn = [ud boolForKey:@"WOSimpleHTTPParserDebugEnabled"];
42 heavyDebugOn = [ud boolForKey:@"WOSimpleHTTPParserHeavyDebugEnabled"];
43 fileIOBoundary = [ud integerForKey:@"WOSimpleHTTPParserFileIOBoundary"];
44 maxUploadSize = [ud integerForKey:@"WOSimpleHTTPParserMaxUploadSizeInKB"];
47 NSLog(@"WOSimpleHTTPParser: max-upload-size: %dKB", maxUploadSize);
48 NSLog(@"WOSimpleHTTPParser: file-IO boundary: %d", fileIOBoundary);
52 - (id)initWithStream:(id<NGStream>)_stream {
53 if (NSStringClass == Nil) NSStringClass = [NSString class];
55 if ((self = [super init])) {
56 if ((self->io = [_stream retain]) == nil) {
61 self->readBytes = (void *)
62 [(NSObject *)self->io methodForSelector:@selector(readBytes:count:)];
63 if (self->readBytes == NULL) {
64 NSLog(@"WARNING(%s): got invalid stream object: %@", __PRETTY_FUNCTION__,
83 [self->content release]; self->content = nil;
84 [self->lastException release]; self->lastException = nil;
85 [self->httpVersion release]; self->httpVersion = nil;
86 [self->headers removeAllObjects];
88 if (self->lineBuffer) {
89 free(self->lineBuffer);
90 self->lineBuffer = NULL;
92 self->lineBufSize = 0;
95 /* low-level reading */
97 - (unsigned int)defaultLineSize {
101 - (NSException *)readNextLine {
104 if (self->lineBuffer == NULL) {
105 self->lineBufSize = [self defaultLineSize];
106 self->lineBuffer = malloc(self->lineBufSize + 10);
109 for (i = 0; YES; i++) {
110 register unsigned rc;
113 rc = self->readBytes(self->io, @selector(readBytes:count:), &c, 1);
116 [self debugWithFormat:@"got result %u, exception: %@",
117 rc, [self->io lastException]];
119 return [self->io lastException];
122 /* check buffer capacity */
123 if ((i + 2) > self->lineBufSize) {
124 static int reallocCount = 0;
126 if (reallocCount > 1000) {
127 static BOOL didLog = NO;
130 NSLog(@"WARNING(%s): reallocated the HTTP line buffer %i times, "
131 @"consider increasing the default line buffer size!",
132 __PRETTY_FUNCTION__, reallocCount);
136 if (self->lineBufSize > (56 * 1024)) {
137 /* to avoid DOS attacks ... */
138 return [NSException exceptionWithName:@"HTTPParserHeaderSizeExceeded"
140 @"got a HTTP line of 100KB+ (DoS attack?)!"
144 self->lineBufSize *= 2;
145 self->lineBuffer = realloc(self->lineBuffer, self->lineBufSize + 10);
152 else if (c == '\r') {
159 self->lineBuffer[i] = c;
162 self->lineBuffer[i] = 0; /* 0-terminate buffer */
164 return nil /* nil means: everything OK */;
167 /* common HTTP parsing */
169 static NSString *ContentLengthHeaderName = @"content-length";
171 static NSString *stringForHeaderName(unsigned char *p) {
175 we try to be smart to avoid creation of NSString objects ...
177 register unsigned len;
178 register unsigned char c1;
180 if ((len = strlen(p)) == 0)
189 if (strcasecmp(p, "te") == 0) return @"te";
190 if (strcasecmp(p, "if") == 0) return @"if";
193 if (strcasecmp(p, "via") == 0) return @"via";
194 if (strcasecmp(p, "age") == 0) return @"age";
195 if (strcasecmp(p, "p3p") == 0) return @"p3p";
200 if (strcasecmp(p, "date") == 0) return @"date";
203 if (strcasecmp(p, "etag") == 0) return @"etag";
206 if (strcasecmp(p, "from") == 0) return @"from";
209 if (strcasecmp(p, "host") == 0) return @"host";
212 if (strcasecmp(p, "vary") == 0) return @"vary";
217 if (strcasecmp(p, "allow") == 0) return @"allow";
218 if (strcasecmp(p, "brief") == 0) return @"brief";
219 if (strcasecmp(p, "range") == 0) return @"range";
220 if (strcasecmp(p, "depth") == 0) return @"depth";
221 if (strcasecmp(p, "ua-os") == 0) return @"ua-os"; /* Entourage */
226 if (strcasecmp(p, "accept") == 0) return @"accept";
229 if (strcasecmp(p, "cookie") == 0) return @"cookie";
232 if (strcasecmp(p, "expect") == 0) return @"expect";
235 if (strcasecmp(p, "pragma") == 0) return @"pragma";
238 if (strcasecmp(p, "server") == 0) return @"server";
241 if (strcasecmp(p, "ua-cpu") == 0) return @"ua-cpu"; /* Entourage */
251 if (strcasecmp(p, "accept-charset") == 0) return @"accept-charset";
252 if (strcasecmp(p, "accept-encoding") == 0) return @"accept-encoding";
253 if (strcasecmp(p, "accept-language") == 0) return @"accept-language";
254 if (strcasecmp(p, "accept-ranges") == 0) return @"accept-ranges";
256 else if (strcasecmp(p, "authorization") == 0)
257 return @"authorization";
264 if (strcasecmp(p, "content-length") == 0)
265 return ContentLengthHeaderName;
267 if (strcasecmp(p, "content-type") == 0) return @"content-type";
268 if (strcasecmp(p, "content-md5") == 0) return @"content-md5";
269 if (strcasecmp(p, "content-range") == 0) return @"content-range";
271 if (strcasecmp(p, "content-encoding") == 0)
272 return @"content-encoding";
273 if (strcasecmp(p, "content-language") == 0)
274 return @"content-language";
276 if (strcasecmp(p, "content-location") == 0)
277 return @"content-location";
278 if (strcasecmp(p, "content-class") == 0) /* Entourage */
279 return @"content-class";
281 else if (strcasecmp(p, "call-back") == 0)
285 if (strcasecmp(p, "connection") == 0) return @"connection";
286 if (strcasecmp(p, "cache-control") == 0) return @"cache-control";
291 if (strcasecmp(p, "destination") == 0) return @"destination";
292 if (strcasecmp(p, "destroy") == 0) return @"destroy";
296 if (strcasecmp(p, "expires") == 0) return @"expires";
297 if (strcasecmp(p, "extension") == 0) return @"extension"; /* Entourage */
301 if (strcasecmp(p, "if-modified-since") == 0)
302 return @"if-modified-since";
303 if (strcasecmp(p, "if-none-match") == 0) /* Entourage */
304 return @"if-none-match";
305 if (strcasecmp(p, "if-match") == 0)
310 if (strcasecmp(p, "keep-alive") == 0) return @"keep-alive";
314 if (strcasecmp(p, "last-modified") == 0) return @"last-modified";
315 if (strcasecmp(p, "location") == 0) return @"location";
316 if (strcasecmp(p, "lock-token") == 0) return @"lock-token";
320 if (strcasecmp(p, "ms-webstorage") == 0) return @"ms-webstorage";
321 if (strcasecmp(p, "max-forwards") == 0) return @"max-forwards";
327 if (strcasecmp(p, "notification-delay") == 0)
328 return @"notification-delay";
329 if (strcasecmp(p, "notification-type") == 0)
330 return @"notification-type";
337 if (strcasecmp(p, "overwrite") == 0)
344 if (strcasecmp(p, "proxy-connection") == 0)
345 return @"proxy-connection";
351 if (strcasecmp(p, "referer") == 0) return @"referer";
358 if (strcasecmp(p, "subscription-lifetime") == 0)
359 return @"subscription-lifetime";
362 if (strcasecmp(p, "subscription-id") == 0)
363 return @"subscription-id";
366 if (strcasecmp(p, "set-cookie") == 0)
367 return @"set-cookie";
373 if (strcasecmp(p, "transfer-encoding") == 0) return @"transfer-encoding";
374 if (strcasecmp(p, "translate") == 0) return @"translate";
375 if (strcasecmp(p, "trailer") == 0) return @"trailer";
376 if (strcasecmp(p, "timeout") == 0) return @"timeout";
380 if (strcasecmp(p, "user-agent") == 0) return @"user-agent";
384 if (strcasecmp(p, "www-authenticate") == 0) return @"www-authenticate";
385 if (strcasecmp(p, "warning") == 0) return @"warning";
389 if ((p[2] == 'w') && (len > 22)) {
390 if (strstr(p, "x-webobjects-") == (void *)p) {
391 p += 13; /* skip x-webobjects- */
392 if (strcmp(p, "server-protocol") == 0)
393 return @"x-webobjects-server-protocol";
394 else if (strcmp(p, "server-protocol") == 0)
395 return @"x-webobjects-server-protocol";
396 else if (strcmp(p, "remote-addr") == 0)
397 return @"x-webobjects-remote-addr";
398 else if (strcmp(p, "remote-host") == 0)
399 return @"x-webobjects-remote-host";
400 else if (strcmp(p, "server-name") == 0)
401 return @"x-webobjects-server-name";
402 else if (strcmp(p, "server-port") == 0)
403 return @"x-webobjects-server-port";
404 else if (strcmp(p, "server-url") == 0)
405 return @"x-webobjects-server-url";
409 if (strcasecmp(p, "x-cache") == 0)
412 else if (len == 12) {
413 if (strcasecmp(p, "x-powered-by") == 0)
414 return @"x-powered-by";
416 if (strcasecmp(p, "x-zidestore-name") == 0)
417 return @"x-zidestore-name";
418 if (strcasecmp(p, "x-forwarded-for") == 0)
419 return @"x-forwarded-for";
420 if (strcasecmp(p, "x-forwarded-host") == 0)
421 return @"x-forwarded-host";
422 if (strcasecmp(p, "x-forwarded-server") == 0)
423 return @"x-forwarded-server";
429 NSLog(@"making custom header name '%s'!", p);
431 /* make name lowercase (we own the buffer, so we can work on it) */
435 for (t = p; *t != '\0'; t++)
438 return [[NSString alloc] initWithCString:p];
441 - (NSException *)parseHeader {
442 NSException *e = nil;
444 while ((e = [self readNextLine]) == nil) {
445 unsigned char *p, *v;
447 NSString *headerName;
448 NSString *headerValue;
451 printf("read header line: '%s'\n", self->lineBuffer);
453 if (strlen(self->lineBuffer) == 0) {
454 /* found end of header */
458 p = self->lineBuffer;
460 if (*p == ' ' || *p == '\t') {
461 // TODO: implement folding (remember last header-key, add string)
463 @"ERROR(%s): got a folded HTTP header line, cannot process!",
464 __PRETTY_FUNCTION__];
468 /* find key/value separator */
469 if ((v = index(p, ':')) == NULL) {
470 [self logWithFormat:@"WARNING: got malformed header line: '%s'",
475 *v = '\0'; v++; /* now 'p' points to name and 'v' to value */
477 /* skip leading spaces */
478 while (*v != '\0' && (*v == ' ' || *v == '\t'))
482 /* trim trailing spaces */
483 for (idx = strlen(v) - 1; idx >= 0; idx--) {
484 if ((v[idx] != ' ' && v[idx] != '\t'))
491 headerName = stringForHeaderName(p);
492 headerValue = [[NSStringClass alloc] initWithCString:v];
494 if (headerName == ContentLengthHeaderName)
495 self->clen = atoi(v);
497 if (headerName != nil || headerValue != nil) {
498 if (self->headers == nil)
499 self->headers = [[NSMutableDictionary alloc] initWithCapacity:32];
501 [self->headers setObject:headerValue forKey:headerName];
504 [headerValue release];
505 [headerName release];
511 - (NSException *)parseEntityOfMethod:(NSString *)_method {
513 TODO: several cases are caught:
514 a) content-length = 0 => empty data
515 b) content-length small => read into memory
516 c) content-length large => streamed into the filesystem to safe RAM
517 d) content-length unknown => ??
520 if (self->clen == 0) {
523 else if (self->clen < 0) {
524 /* I think HTTP/1.1 requires a content-length header to be present ? */
526 if ([self->httpVersion isEqualToString:@"HTTP/1.0"] ||
527 [self->httpVersion isEqualToString:@"HTTP/0.9"]) {
528 /* content-length unknown, read till EOF */
529 BOOL readToEOF = YES;
531 if ([_method isEqualToString:@"HEAD"])
533 else if ([_method isEqualToString:@"GET"])
535 else if ([_method isEqualToString:@"DELETE"])
540 @"WARNING: not processing entity of request "
541 @"without contentlen!"];
545 else if (self->clen > maxUploadSize*1024) {
546 /* entity is too large */
547 return [NSException exceptionWithName:@"LimitException"
549 @"the maximum HTTP transaction size was exceeded"
552 else if (self->clen > fileIOBoundary) {
553 /* we are streaming the content to a file and use a memory mapped data */
561 [self debugWithFormat:@"streaming %i bytes into file ...", self->clen];
563 fn = [[NSProcessInfo processInfo] temporaryFileName];
565 if ((t = fopen([fn cString], "w")) == NULL) {
566 [self logWithFormat:@"ERROR: could not open temporary file '%@'!", fn];
568 /* read into memory as a fallback ... */
571 [[(NGStream *)self->io safeReadDataOfLength:self->clen] retain];
572 if (self->content == nil)
573 return [self->io lastException];
577 for (toGo = self->clen; toGo > 0; ) {
578 unsigned readCount, writeCount;
580 /* read from socket */
581 readCount = [self->io readBytes:buf count:sizeof(buf)];
582 if (readCount == NGStreamError) {
590 if ((writeCount = fwrite(buf, readCount, 1, t)) != 1) {
593 writeError = ferror(t);
600 unlink([fn cString]); /* delete temporary file */
602 if (writeError == 0) {
603 return [NSException exceptionWithName:@"SystemWriteError"
604 reason:@"failed to write data to upload file"
608 return [self->io lastException];
611 self->content = [[NSData alloc] initWithContentsOfMappedFile:fn];
612 unlink([fn cString]); /* if the mmap disappears, the storage is freed */
615 /* content-length known and small */
616 //[self logWithFormat:@"reading %i bytes of the entity", self->clen];
619 [[(NGStream *)self->io safeReadDataOfLength:self->clen] retain];
620 if (self->content == nil)
621 return [self->io lastException];
623 //[self logWithFormat:@"read %i bytes.", [self->content length]];
629 /* handling expectations */
631 - (BOOL)processContinueExpectation {
632 // TODO: this should check the credentials of a request before accepting the
633 // body. The current implementation is far from optimal and only added
634 // for Mono compatibility (and actually produces the same behaviour
635 // like with HTTP/1.0 ...)
636 static unsigned char *contStatLine =
637 "HTTP/1.0 100 Continue\r\n"
638 "content-length: 0\r\n"
640 static unsigned char *failStatLine =
641 "HTTP/1.0 417 Expectation Failed\r\n"
642 "content-length: 0\r\n"
644 unsigned char *respline = NULL;
647 [self debugWithFormat:@"process 100 continue on IO: %@", self->io];
649 if (self->clen > 0 && (self->clen > (maxUploadSize * 1024))) {
650 // TODO: return a 417 expectation failed
652 respline = failStatLine;
656 respline = contStatLine;
659 if (![self->io safeWriteBytes:respline count:strlen(respline)]) {
660 ASSIGN(self->lastException, [self->io lastException]);
663 if (![self->io flush]) {
664 ASSIGN(self->lastException, [self->io lastException]);
673 - (WORequest *)parseRequest {
674 NSException *e = nil;
676 NSString *uri = @"/";
677 NSString *method = @"GET";
682 [self logWithFormat:@"HeavyDebug: parsing response ..."];
684 /* process request line */
686 if ((e = [self readNextLine])) {
687 ASSIGN(self->lastException, e);
691 printf("read request line: '%s'\n", self->lineBuffer);
694 /* sample line: "GET / HTTP/1.0" */
695 unsigned char *p, *t;
699 p = self->lineBuffer;
700 if ((t = index(p, ' ')) == NULL) {
701 [self logWithFormat:@"got broken request line '%s'", self->lineBuffer];
707 /* intended fall-throughs ! */
709 if (strcasecmp(p, "BPROPFIND") == 0) { method = @"BPROPFIND"; break; }
710 if (strcasecmp(p, "BPROPPATCH") == 0) { method = @"BPROPPATCH"; break; }
712 if (strcasecmp(p, "COPY") == 0) { method = @"COPY"; break; }
713 if (strcasecmp(p, "CHECKOUT") == 0) { method = @"CHECKOUT"; break; }
714 if (strcasecmp(p, "CHECKIN") == 0) { method = @"CHECKIN"; break; }
716 if (strcasecmp(p, "DELETE") == 0) { method = @"DELETE"; break; }
718 if (strcasecmp(p, "HEAD") == 0) { method = @"HEAD"; break; }
720 if (strcasecmp(p, "LOCK") == 0) { method = @"LOCK"; break; }
722 if (strcasecmp(p, "GET") == 0) { method = @"GET"; break; }
724 if (strcasecmp(p, "MKCOL") == 0) { method = @"MKCOL"; break; }
725 if (strcasecmp(p, "MOVE") == 0) { method = @"MOVE"; break; }
727 if (strcasecmp(p, "NOTIFY") == 0) { method = @"NOTIFY"; break; }
729 if (strcasecmp(p, "OPTIONS") == 0) { method = @"OPTIONS"; break; }
731 if (strcasecmp(p, "PUT") == 0) { method = @"PUT"; break; }
732 if (strcasecmp(p, "POST") == 0) { method = @"POST"; break; }
733 if (strcasecmp(p, "PROPFIND") == 0) { method = @"PROPFIND"; break; }
734 if (strcasecmp(p, "PROPPATCH") == 0) { method = @"PROPPATCH"; break; }
735 if (strcasecmp(p, "POLL") == 0) { method = @"POLL"; break; }
737 if (strcasecmp(p, "REPORT") == 0) { method = @"REPORT"; break; }
739 if (strcasecmp(p, "SEARCH") == 0) { method = @"SEARCH"; break; }
740 if (strcasecmp(p, "SUBSCRIBE") == 0) { method = @"SUBSCRIBE"; break; }
742 if (strcasecmp(p, "UNLOCK") == 0) { method = @"UNLOCK"; break; }
743 if (strcasecmp(p, "UNSUBSCRIBE")== 0) { method = @"UNSUBSCRIBE"; break; }
744 if (strcasecmp(p, "UNCHECKOUT") == 0) { method = @"UNCHECKOUT"; break; }
746 if (strcasecmp(p, "VERSION-CONTROL") == 0) {
747 method = @"VERSION-CONTROL";
753 [self debugWithFormat:@"making custom HTTP method name: '%s'", p];
754 method = [NSString stringWithCString:p];
760 p = t + 1; /* skip space */
761 while (*p != '\0' && (*p == ' ' || *p == '\t')) /* skip spaces */
765 [self logWithFormat:@"got broken request line '%s'", self->lineBuffer];
769 if ((t = index(p, ' ')) == NULL) {
770 /* the URI isn't followed by a HTTP version */
771 self->httpVersion = @"HTTP/0.9";
772 /* TODO: strip trailing spaces for better compliance */
773 uri = [NSString stringWithCString:p];
777 uri = [NSString stringWithCString:p];
781 p = t + 1; /* skip space */
782 while (*p != '\0' && (*p == ' ' || *p == '\t')) /* skip spaces */
786 self->httpVersion = @"HTTP/0.9";
787 else if (strcasecmp(p, "http/1.0") == 0)
788 self->httpVersion = @"HTTP/1.0";
789 else if (strcasecmp(p, "http/1.1") == 0)
790 self->httpVersion = @"HTTP/1.1";
792 /* TODO: strip trailing spaces */
793 self->httpVersion = [[NSString alloc] initWithCString:p];
800 if ((e = [self parseHeader])) {
801 ASSIGN(self->lastException, e);
805 [self logWithFormat:@"parsed header: %@", self->headers];
807 /* check for expectations */
809 if ((expect = [self->headers objectForKey:@"expect"])) {
810 if ([expect rangeOfString:@"100-continue"
811 options:NSCaseInsensitiveSearch].length > 0) {
812 if (![self processContinueExpectation])
820 if ((e = [self parseEntityOfMethod:method])) {
821 ASSIGN(self->lastException, e);
827 [self logWithFormat:@"HeavyDebug: got all .."];
829 r = [[WORequest alloc] initWithMethod:method
831 httpVersion:self->httpVersion
832 headers:self->headers
833 content:self->content
838 [self logWithFormat:@"HeavyDebug: request: %@", r];
840 return [r autorelease];
843 - (WOResponse *)parseResponse {
844 NSException *e = nil;
850 [self logWithFormat:@"HeavyDebug: parsing response ..."];
852 /* process response line */
854 if ((e = [self readNextLine])) {
855 ASSIGN(self->lastException, e);
859 printf("read response line: '%s'\n", self->lineBuffer);
862 /* sample line: "HTTP/1.0 200 OK" */
863 unsigned char *p, *t;
867 p = self->lineBuffer;
868 if ((t = index(p, ' ')) == NULL) {
869 [self logWithFormat:@"got broken response line '%s'", self->lineBuffer];
874 if (strcasecmp(p, "http/1.0") == 0)
875 self->httpVersion = @"HTTP/1.0";
876 else if (strcasecmp(p, "http/1.1") == 0)
877 self->httpVersion = @"HTTP/1.1";
879 self->httpVersion = [[NSString alloc] initWithCString:p];
883 p = t + 1; /* skip space */
884 while (*p != '\0' && (*p == ' ' || *p == '\t')) /* skip spaces */
887 [self logWithFormat:@"got broken response line '%s'", self->lineBuffer];
892 /* we don't need to parse a reason ... */
897 if ((e = [self parseHeader])) {
898 ASSIGN(self->lastException, e);
902 [self logWithFormat:@"parsed header: %@", self->headers];
907 if ((e = [self parseEntityOfMethod:nil /* parsing a response */])) {
908 ASSIGN(self->lastException, e);
914 [self logWithFormat:@"HeavyDebug: got all .."];
916 r = [[[WOResponse alloc] init] autorelease];
918 [r setHTTPVersion:self->httpVersion];
919 [r setHeaders:self->headers];
920 [r setContent:self->content];
925 [self logWithFormat:@"HeavyDebug: response: %@", r];
930 - (NSException *)lastException {
931 return self->lastException;
936 - (BOOL)isDebuggingEnabled {
940 @end /* WOSimpleHTTPParser */