mochiweb源码阅读6-request模块常用函数说明

来源:互联网 发布:淘宝自定义属性怎么写 编辑:程序博客网 时间:2024/03/29 06:37

回复客户端函数:
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

0 0
原创粉丝点击