diff --git a/Common/HttpParser.swift b/Common/HttpParser.swift index f7d3e265..26f9eec8 100644 --- a/Common/HttpParser.swift +++ b/Common/HttpParser.swift @@ -20,9 +20,23 @@ class HttpParser { if error != nil { error.memory = err("Invalid status line: \(statusLine)") } return nil } + var addr = UnsafeMutablePointer.alloc(1) + var len = socklen_t(sizeofValue(addr.memory)) let method = statusTokens[0] let path = statusTokens[1] let urlParams = extractUrlParams(path) + var host:String = "" + if getpeername(socket, UnsafeMutablePointer(addr), &len) != -1 { + + var hostBuffer = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0) + if (getnameinfo(UnsafeMutablePointer(addr), socklen_t(addr.memory.ss_len), + &hostBuffer, socklen_t(hostBuffer.count), nil, 0, + NI_NUMERICHOST) == 0) { + host = String.fromCString(hostBuffer)! + //println("socket \(sockfd) ip \(host)") + } + } + addr.dealloc(1) // TODO extract query parameters if let headers = nextHeaders(socket, error: error) { // TODO detect content-type and handle: @@ -30,9 +44,12 @@ class HttpParser { // 'multipart' -> Dictionary if let contentSize = headers["content-length"]?.toInt() { let body = nextBody(socket, size: contentSize, error: error) - return HttpRequest(url: path, urlParams: urlParams, method: method, headers: headers, body: body, capturedUrlGroups: []) + + return HttpRequest(remoteAddress:host, url: path, urlParams: urlParams, method: method, headers: headers, body: body, capturedUrlGroups: []) + } - return HttpRequest(url: path, urlParams: urlParams, method: method, headers: headers, body: nil, capturedUrlGroups: []) + return HttpRequest(remoteAddress:host, url: path, urlParams: urlParams, method: method, headers: headers, body: nil, capturedUrlGroups: []) + } } return nil diff --git a/Common/HttpRequest.swift b/Common/HttpRequest.swift index 03db3fbf..108734b4 100644 --- a/Common/HttpRequest.swift +++ b/Common/HttpRequest.swift @@ -6,7 +6,8 @@ import Foundation -struct HttpRequest { +struct HttpRequest { + let remoteAddress:String let url: String let urlParams: [(String, String)] // http://stackoverflow.com/questions/1746507/authoritative-position-of-duplicate-http-get-query-keys let method: String