Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jigzat #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Common/HttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,36 @@ class HttpParser {
if error != nil { error.memory = err("Invalid status line: \(statusLine)") }
return nil
}
var addr = UnsafeMutablePointer<sockaddr_storage>.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:
// 'application/x-www-form-urlencoded' -> Dictionary
// '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
Expand Down
3 changes: 2 additions & 1 deletion Common/HttpRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down