HTTPServerRequest

public class HTTPServerRequest : ServerRequest

This class implements the ServerRequest protocol for incoming sockets that are communicating via the HTTP protocol. Data and Strings can be read in.

Usage Example:

 func handlePost(request: ServerRequest, response: ServerResponse) {
     var body = Data()
     do {
         let length = try request.readAllData(into: &body)
         let result = "Read \(length) bytes"
         response.headers["Content-Type"] = ["text/plain"]
         response.headers["Content-Length"] = ["\(result.count)"]

         try response.end(text: result)
     }
     catch {
         print("Error reading body or writing response")
     }
 }
  • HTTP Status code if this message is a response

    Usage Example:

    response!.httpStatusCode
    

    Declaration

    Swift

    @available(*, deprecated, message: "This method never worked on Server Requests and was inherited incorrectly from a super class")
    public private(set) var httpStatusCode: HTTPStatusCode { get }
  • Server IP address pulled from socket.

    Usage Example:

    request.remoteAddress
    

    Declaration

    Swift

    public var remoteAddress: String { get }
  • HTTP Method of the request.

    Usage Example:

    request.method.lowercased()
    

    Declaration

    Swift

    public var method: String { get }
  • Major version of HTTP of the request

    Usage Example:

    print(String(describing: request.httpVersionMajor))
    

    Declaration

    Swift

    public var httpVersionMajor: UInt16? { get }
  • Minor version of HTTP of the request

    Usage Example:

    print(String(describing: request.httpVersionMinor))
    

    Declaration

    Swift

    public var httpVersionMinor: UInt16? { get }
  • Set of HTTP headers of the request.

    Usage Example:

    let protocols = request.headers["Upgrade"]
    

    Declaration

    Swift

    public var headers: HeadersContainer { get }
  • socket signature of the request.

    Usage Example:

    public var signature: Socket.Signature? { return socket.signature }
    

    Declaration

    Swift

    public var signature: Socket.Signature? { get }
  • Create and validate the full URL.

    Usage Example:

    print(request.urlURL)
    

    Declaration

    Swift

    public var urlURL: URL { get }
  • The URL from the request as URLComponents URLComponents has a memory leak on linux as of swift 3.0.1. Use ‘urlURL’ instead

    Usage Example:

    print(request.urlComponents)
    

    Declaration

    Swift

    @available(*, deprecated, message: "URLComponents has a memory leak on linux as of swift 3.0.1. use 'urlURL' instead")
    public var urlComponents: URLComponents { get }
  • The URL from the request in string form This contains just the path and query parameters starting with ‘/’ Use ‘urlURL’ for the full URL

    Usage Example:

    print(request.urlString)
    

    Declaration

    Swift

    @available(*, deprecated, message: "This contains just the path and query parameters starting with '/'. use 'urlURL' instead")
    public var urlString: String { get }
  • url

    The URL from the request in UTF-8 form This contains just the path and query parameters starting with ‘/’ Use ‘urlURL’ for the full URL

    Usage Example:

    print(request.url)
    

    Declaration

    Swift

    public var url: Data { get }
  • Read a chunk of the body of the request.

    Throws

    if an error occurs while reading the body.

    Usage Example:

    let readData = try self.read(into: data)
    

    Declaration

    Swift

    public func read(into data: inout Data) throws -> Int

    Parameters

    into

    An NSMutableData to hold the data in the request.

    Return Value

    the number of bytes read.

  • Read the whole body of the request.

    Throws

    if an error occurs while reading the data.

    Usage Example:

    let length = try request.readAllData(into: &body)
    

    Declaration

    Swift

    @discardableResult
    public func readAllData(into data: inout Data) throws -> Int

    Parameters

    into

    An NSMutableData to hold the data in the request.

    Return Value

    the number of bytes read.

  • Read a chunk of the body and return it as a String.

    Throws

    if an error occurs while reading the data.

    Usage Example:

    let body = try request.readString()
    

    Declaration

    Swift

    public func readString() throws -> String?

    Return Value

    an Optional string.