简体   繁体   中英

How to parse a raw HTTP request

The question is simple. In AutoHotkey, how would you parse a raw HTTP request to access data like the method, http version, cookies and host.

;Example of a raw http request:

rawHttp =
(
POST https://www.example.com/Login HTTP/1.1
Host: www.example.com
Connection: Keep-Alive
Cookie: session=b5j2h46fdthr46t74g5g234g5f3g6753kj73l

username=lalala&password=12345&rememberMe=1
)

;Example usage of the function below:

httpObj := HttpTxtToObj(rawHttp)
MsgBox % "Request method: " httpObj.requestLine.method
MsgBox % "Request URI: " httpObj.requestLine.uri
MsgBox % "Request http version: " httpObj.requestLine.httpVersion
MsgBox % "Request header (Host): " httpObj.headers["Host"]
MsgBox % "Request header (Connection): " httpObj.headers["Connection"]
MsgBox % "Request header (Cookie): " httpObj.headers["Cookie"]
MsgBox % "Request body: " httpObj.body

;This function takes care of everything:

HttpTxtToObj(rawHttp) {
    ;Split request into "request line", "headers" and "body"(if existent)
    RegExMatch(rawHttp, "OU)^(?P<requestLine>.+)\R(?P<headers>.+)\R\R(?P<body>.*)$",request)
    If !request.Count()
        RegExMatch(rawHttp, "OU)^(?P<requestLine>.+)\R(?P<headers>.+)$",request)

    ;Split request line into "method" "requestUrl" and "httpVersion"
    RegExMatch(request.requestLine, "OU)^(?P<method>[^\s]+)\s(?P<uri>[^\s]+)\s(?P<httpVersion>.+)$", requestLine)

    ;Make a nice key value array for the headers:
    headers := {}
    While (p := RegexMatch(request.headers, "OSU)(?P<key>[^:]+):\s(?P<value>[^\R]+(\R|$))", currentHeader, p?p+1:1))
        headers.Insert(currentHeader.key, currentHeader.value)

    ;The body is usually a query string, json string or just text. When you uplaod a file it may even contain binary data.
    ;All that would make the code quite a bit more complex, so for now we'll just pretend it's normal text, nothing special.

    ;Now lets return a nice multidimensional array 
    Return {requestLine: requestLine, headers: headers, body: request.body}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM