Differences From Artifact [abf9a790cd]:
- File
src/OFHTTPServer.m
— part of check-in
[25c985fec1]
at
2020-08-25 00:11:24
on branch trunk
— OFNumber: Remove (u)int{8,16,32,64} methods
Since C guarantees minimum sizes for char, short, int, long and long
long, these can be used instead. (user: js, size: 21686) [annotate] [blame] [check-ins using] [more...]
To Artifact [ac3250a22a]:
- File
src/OFHTTPServer.m
— part of check-in
[a2b4238850]
at
2020-11-06 02:24:14
on branch trunk
— Future-proof some code using freeWhenDone
While -[OFData initWithDataNoCopy:count:itemSize:freeWhenDone:] can
currently never throw, it might in the future, and when it does so in
the future, these would be memory leaks then. (user: js, size: 21471) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
143 144 145 146 147 148 149 | ? of_ascii_toupper(*tmp) : of_ascii_tolower(*tmp)); firstLetter = false; tmp++; } | > | | > > > > | 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | ? of_ascii_toupper(*tmp) : of_ascii_tolower(*tmp)); firstLetter = false; tmp++; } @try { return [OFString stringWithUTF8StringNoCopy: cString freeWhenDone: true]; } @catch (id e) { free(cString); @throw e; } } @implementation OFHTTPServerResponse - (instancetype)initWithSocket: (OFStreamSocket *)sock server: (OFHTTPServer *)server request: (OFHTTPRequest *)request { |
︙ | ︙ | |||
180 181 182 183 184 185 186 | - (void)of_sendHeaders { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary OF_GENERIC(OFString *, OFString *) *headers; OFEnumerator *keyEnumerator, *valueEnumerator; OFString *key, *value; | | | 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | - (void)of_sendHeaders { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary OF_GENERIC(OFString *, OFString *) *headers; OFEnumerator *keyEnumerator, *valueEnumerator; OFString *key, *value; [_socket writeFormat: @"HTTP/%@ %hd %@\r\n", self.protocolVersionString, _statusCode, of_http_status_code_to_string(_statusCode)]; headers = [[_headers mutableCopy] autorelease]; if ([headers objectForKey: @"Date"] == nil) { OFString *date = [[OFDate date] |
︙ | ︙ | |||
372 373 374 375 376 377 378 | return [self sendErrorAndClose: 400]; } pos = [line rangeOfString: @" "].location; if (pos == OF_NOT_FOUND) return [self sendErrorAndClose: 400]; | | | 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | return [self sendErrorAndClose: 400]; } pos = [line rangeOfString: @" "].location; if (pos == OF_NOT_FOUND) return [self sendErrorAndClose: 400]; method = [line substringToIndex: pos]; @try { _method = of_http_request_method_from_string(method); } @catch (OFInvalidArgumentException *e) { return [self sendErrorAndClose: 405]; } @try { |
︙ | ︙ | |||
448 449 450 451 452 453 454 | return false; } pos = [line rangeOfString: @":"].location; if (pos == OF_NOT_FOUND) return [self sendErrorAndClose: 400]; | | | < | < < < | | 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | return false; } pos = [line rangeOfString: @":"].location; if (pos == OF_NOT_FOUND) return [self sendErrorAndClose: 400]; key = [line substringToIndex: pos]; value = [line substringFromIndex: pos + 1]; key = normalizedKey(key.stringByDeletingTrailingWhitespaces); value = value.stringByDeletingLeadingWhitespaces; old = [_headers objectForKey: key]; if (old != nil) value = [old stringByAppendingFormat: @",%@", value]; [_headers setObject: value forKey: key]; if ([key isEqual: @"Host"]) { pos = [value rangeOfString: @":" options: OF_STRING_SEARCH_BACKWARDS].location; if (pos != OF_NOT_FOUND) { [_host release]; _host = [[value substringToIndex: pos] retain]; @try { unsigned long long portTmp = [value substringFromIndex: pos + 1] .unsignedLongLongValue; if (portTmp < 1 || portTmp > UINT16_MAX) return [self sendErrorAndClose: 400]; _port = (uint16_t)portTmp; } @catch (OFInvalidFormatException *e) { |
︙ | ︙ | |||
501 502 503 504 505 506 507 | } - (bool)sendErrorAndClose: (short)statusCode { OFString *date = [[OFDate date] dateStringWithFormat: @"%a, %d %b %Y %H:%M:%S GMT"]; | | | 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | } - (bool)sendErrorAndClose: (short)statusCode { OFString *date = [[OFDate date] dateStringWithFormat: @"%a, %d %b %Y %H:%M:%S GMT"]; [_socket writeFormat: @"HTTP/1.1 %hd %@\r\n" @"Date: %@\r\n" @"Server: %@\r\n" @"\r\n", statusCode, of_http_status_code_to_string(statusCode), date, _server.name]; |
︙ | ︙ | |||
544 545 546 547 548 549 550 | URL.host = _host; if (_port != 80) URL.port = [OFNumber numberWithUnsignedShort: _port]; if ((pos = [_path rangeOfString: @"?"].location) != OF_NOT_FOUND) { OFString *path, *query; | | | < | 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | URL.host = _host; if (_port != 80) URL.port = [OFNumber numberWithUnsignedShort: _port]; if ((pos = [_path rangeOfString: @"?"].location) != OF_NOT_FOUND) { OFString *path, *query; path = [_path substringToIndex: pos]; query = [_path substringFromIndex: pos + 1]; URL.URLEncodedPath = path; URL.URLEncodedQuery = query; } else URL.URLEncodedPath = _path; [URL makeImmutable]; |
︙ | ︙ | |||
694 695 696 697 698 699 700 | if (_toRead == 0) _toRead = -2; return length; } else { void *pool = objc_autoreleasePoolPush(); OFString *line; | | | | | < | < | 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | if (_toRead == 0) _toRead = -2; return length; } else { void *pool = objc_autoreleasePoolPush(); OFString *line; size_t pos; unsigned long long toRead; @try { line = [_socket tryReadLine]; } @catch (OFInvalidEncodingException *e) { @throw [OFInvalidFormatException exception]; } if (line == nil) return 0; pos = [line rangeOfString: @";"].location; if (pos != OF_NOT_FOUND) line = [line substringToIndex: pos]; if (line.length < 1) { /* * We have read the empty string because the socket is * at end of stream. */ if (_socket.atEndOfStream && pos == OF_NOT_FOUND) @throw [OFTruncatedDataException exception]; else @throw [OFInvalidFormatException exception]; } toRead = [line unsignedLongLongValueWithBase: 16]; if (toRead > LLONG_MAX) |
︙ | ︙ |