@@ -483,11 +483,9 @@ class _HTTPServer {
483
483
484
484
struct _HTTPRequest {
485
485
enum Method : String {
486
- case HEAD
487
486
case GET
488
487
case POST
489
488
case PUT
490
- case DELETE
491
489
}
492
490
let method : Method
493
491
let uri : String
@@ -497,22 +495,14 @@ struct _HTTPRequest {
497
495
let headers : [ String ]
498
496
499
497
enum Error : Swift . Error {
500
- case invalidURI
501
- case invalidMethod
502
498
case headerEndNotFound
503
499
}
504
500
505
501
public init ( header: String ) throws {
506
502
headers = header. components ( separatedBy: _HTTPUtils. CRLF)
507
- guard headers. count > 0 else {
508
- throw Error . invalidURI
509
- }
510
- let uriParts = headers [ 0 ] . components ( separatedBy: " " )
511
- guard uriParts. count > 2 , let methodName = Method ( rawValue: uriParts [ 0 ] ) else {
512
- throw Error . invalidMethod
513
- }
514
- method = methodName
515
- uri = uriParts [ 1 ]
503
+ let action = headers [ 0 ]
504
+ method = Method ( rawValue: action. components ( separatedBy: " " ) [ 0 ] ) !
505
+ uri = action. components ( separatedBy: " " ) [ 1 ]
516
506
body = " "
517
507
}
518
508
@@ -534,31 +524,14 @@ struct _HTTPRequest {
534
524
}
535
525
return nil
536
526
}
537
-
538
- public func headersAsJSON( ) throws -> Data {
539
- var headerDict : [ String : String ] = [ : ]
540
- for header in headers {
541
- if header. hasPrefix ( method. rawValue) {
542
- headerDict [ " uri " ] = header
543
- continue
544
- }
545
- let parts = header. components ( separatedBy: " : " )
546
- if parts. count > 1 {
547
- headerDict [ parts [ 0 ] ] = parts [ 1 ] . trimmingCharacters ( in: CharacterSet ( charactersIn: " " ) )
548
- }
549
- }
550
- return try JSONEncoder ( ) . encode ( headerDict)
551
- }
552
527
}
553
528
554
529
struct _HTTPResponse {
555
530
enum Response : Int {
556
531
case OK = 200
557
532
case REDIRECT = 302
558
- case BAD_REQUEST = 400
559
533
case NOTFOUND = 404
560
534
case METHOD_NOT_ALLOWED = 405
561
- case SERVER_ERROR = 500
562
535
}
563
536
private let responseCode : Response
564
537
private let headers : String
@@ -627,56 +600,20 @@ public class TestURLSessionServer {
627
600
} else if req. uri. hasPrefix ( " /unauthorized " ) {
628
601
try httpServer. respondWithUnauthorizedHeader ( )
629
602
} else {
630
- try httpServer. respond ( with: getResponse ( request: req) )
631
- }
632
- }
633
-
634
- func getResponse( request: _HTTPRequest ) throws -> _HTTPResponse {
635
-
636
- func headersAsJSONResponse( ) throws -> _HTTPResponse {
637
- guard let body = try String ( data: request. headersAsJSON ( ) , encoding: . utf8) else {
638
- return _HTTPResponse ( response: . SERVER_ERROR, body: " Cant convert headers to JSON object " )
603
+ if req. method == . GET || req. method == . POST || req. method == . PUT {
604
+ try httpServer. respond ( with: getResponse ( request: req) )
605
+ }
606
+ else {
607
+ try httpServer. respond ( with: _HTTPResponse ( response: . METHOD_NOT_ALLOWED, body: " Method not allowed " ) )
639
608
}
640
- let headers = " Content-Type: application/json \r \n Content-Length: \( body. count) "
641
- return _HTTPResponse ( response: . OK, headers: headers, body: body)
642
609
}
610
+ }
643
611
612
+ func getResponse( request: _HTTPRequest ) -> _HTTPResponse {
644
613
let uri = request. uri
645
- if uri == " /head " {
646
- guard request. method == . HEAD else { return _HTTPResponse ( response: . METHOD_NOT_ALLOWED, body: " Method not allowed " ) }
647
- return try headersAsJSONResponse ( )
648
- }
649
-
650
- if uri == " /get " {
651
- guard request. method == . GET else { return _HTTPResponse ( response: . METHOD_NOT_ALLOWED, body: " Method not allowed " ) }
652
- return try headersAsJSONResponse ( )
653
- }
654
-
655
- if uri == " /put " {
656
- guard request. method == . PUT else { return _HTTPResponse ( response: . METHOD_NOT_ALLOWED, body: " Method not allowed " ) }
657
- return try headersAsJSONResponse ( )
658
- }
659
-
660
- if uri == " /post " {
661
- guard request. method == . POST else { return _HTTPResponse ( response: . METHOD_NOT_ALLOWED, body: " Method not allowed " ) }
662
- return try headersAsJSONResponse ( )
663
- }
664
-
665
- if uri == " /delete " {
666
- guard request. method == . DELETE else { return _HTTPResponse ( response: . METHOD_NOT_ALLOWED, body: " Method not allowed " ) }
667
- return try headersAsJSONResponse ( )
668
- }
669
-
670
614
if uri == " /upload " {
671
- if let contentLength = request. getHeader ( for: " content-length " ) {
672
- let text = " Upload completed!, Content-Length: \( contentLength) "
673
- return _HTTPResponse ( response: . OK, headers: " Content-Length: \( text. data ( using: . utf8) !. count) " , body: text)
674
- }
675
- if let te = request. getHeader ( for: " transfer-encoding " ) , te == " chunked " {
676
- return _HTTPResponse ( response: . OK, body: " Received Chunked request " )
677
- } else {
678
- return _HTTPResponse ( response: . BAD_REQUEST, body: " Missing Content-Length " )
679
- }
615
+ let text = " Upload completed! "
616
+ return _HTTPResponse ( response: . OK, headers: " Content-Length: \( text. data ( using: . utf8) !. count) " , body: text)
680
617
}
681
618
682
619
if uri == " /country.txt " {
0 commit comments