From f29b3e44fa6e8a7d9585ab3cf16cd438e724813e Mon Sep 17 00:00:00 2001 From: Samir Gartner Date: Fri, 27 Mar 2015 12:51:44 -0500 Subject: [PATCH 1/2] Update HttpRequest.swift added remoteAddress parameter, sorry for the long name, I really like verbosity. --- Common/HttpRequest.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From db361ecd8a72ac01c56efaa60ee8e139b21d139c Mon Sep 17 00:00:00 2001 From: Samir Gartner Date: Fri, 27 Mar 2015 12:54:43 -0500 Subject: [PATCH 2/2] Update HttpParser.swift --- Common/HttpParser.swift | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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