mochiweb源碼閱讀6-request模塊常用函數說明

回覆客戶端函數:
1 Req:respond({Code,ResponseHeaders,Body}) ->response()
2 Req:serve_file(Paht,DocRoot) ->response()
3 Req:ok({ContentType, ResponseHeaders, Body}) ->response()
4 Req:not_found() ->response()
(注:回覆客戶端函數詳細說明可以看前篇mochiweb源碼閱讀4-概括與使用)
獲取http請求信息函數:
1 Req:get(socket) ->Socket 返回Socket
2 Req:get(method) ->’OPTIONS’|’GET’|’HEAD’|’POST’|’PUT’|’DELETE’|’TRACE’ 獲取http請求方式
3 Req:get(path) ->string() 返回路徑
例:請求連接爲localhost:8080/funtest,則path爲”funtest”
4 Req:get(raw_path) ->string() 獲取原路徑
例:請求連接爲localhost:8080/funtest,則raw_path爲”/funtest”
5 Req:get(version) ->tuple 返回http請求版本 例:{1,1}
6 Req:get(headers) ->dict(). 返回文件頭信息
例:瀏覽器訪問本機,連接爲localhost:8080/funtest

{7,
    {"host",
        {'Host',"localhost:8080"},
        {"accept",
            {'Accept',"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
        nil,
        {"accept-language",
            {'Accept-Language',"en-US,en;q=0.5"},
            {"accept-encoding",{'Accept-Encoding',"gzip, deflate"},nil,nil},
            {"connection",{'Connection',"keep-alive"},nil,nil}}},
    {"user-agent",
        {'User-Agent',"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"},
        {"upgrade-insecure-requests",
            {"Upgrade-Insecure-Requests","1"},
            nil,nil},
    nil}}}

7 Req:get_header_value(Key) ->undefined|string() 獲取某個header,如函數6所示,若Key爲“User-Agent”則返回“Mozilla…”
8 Req:get(peer) ->string() 返回客戶端ip
例如:請求連接爲localhost:8080/funtest,則peer=”127.0.0.1”
9 Req:parse_qs() ->[{string(),stringg()}] 獲取get參數值
例:請求連接爲localhost:8080/funtest?id=5,則返回[“id”,”5”]
10 Req:parse_cookie() ->[{string(),string()}] 解析cookie
11.Req:parse_post() 獲取表單提取方式的body
12.Req:recv_body() 獲取請求body

發佈了48 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章