API
#net/http
Xu 0.8.8 API
import: #net/http
counts: 19 函数, 46 方法, 8 类型, 0 常量, 0 方法集
HTTP/1.1 Client 与 Server、Header、Cookie。本包无 TLS。
struct Response
pub struct Response {
pub status_code int
pub status string
pub headers string
pub body string
pub sticky string
}
用途
公开结构体 `Response`。
参数
status_code int- 字段(int)
status string- 字段(string)
headers string- 字段(string)
body string- 正文
sticky string- 字段(string)
new_response
pub fn new_response() Response
用途
构造 response。
参数
无
返回
Response — 该类型的值
(Response) status_code
pub fn (r Response) status_code() int
用途
`Response` 上的方法:status码。
参数
r Response- 接收者 Response(值接收者)
返回
int — 该类型的值
(Response) status
pub fn (r Response) status() string
用途
在 `Response` 上调用 `status`。
参数
r Response- 接收者 Response(值接收者)
返回
string — 结果字符串
(Response) body
pub fn (r Response) body() string
用途
`Response` 上的方法:正文。
参数
r Response- 接收者 Response(值接收者)
返回
string — 结果字符串
struct Request
pub struct Request {
pub method string
pub path string
pub headers string
pub body string
pub sticky string
}
用途
公开结构体 `Request`。
参数
method string- 字段(string)
path string- 文件系统路径
headers string- 字段(string)
body string- 正文
sticky string- 字段(string)
new_request
pub fn new_request() Request
用途
构造 request。
参数
无
返回
Request — 该类型的值
(Request) method
pub fn (r Request) method() string
用途
在 `Request` 上调用 `method`。
参数
r Request- 接收者 Request(值接收者)
返回
string — 结果字符串
(Request) path
pub fn (r Request) path() string
用途
在 `Request` 上调用 `path`。
参数
r Request- 接收者 Request(值接收者)
返回
string — 结果字符串
(Request) body
pub fn (r Request) body() string
用途
`Request` 上的方法:正文。
参数
r Request- 接收者 Request(值接收者)
返回
string — 结果字符串
(Request) user_agent
pub fn (r Request) user_agent() string
用途
`Request` 上的方法:用户agent。
参数
r Request- 接收者 Request(值接收者)
返回
string — 结果字符串
(Request) host
pub fn (r Request) host() string
用途
`Request` 上的方法:主机。
参数
r Request- 接收者 Request(值接收者)
返回
string — 结果字符串
struct Server
pub struct Server {
pub lfd int
}
用途
公开结构体 `Server`。
参数
lfd int- 字段(int)
new_server
pub fn new_server() Server
用途
构造 server。
参数
无
返回
Server — 该类型的值
(&mut Server) listen
pub fn (s &mut Server) listen(addr string) int
用途
`&mut Server` 上的方法:开始监听。失败时 throw。
参数
s &mut Server- 接收者 &mut Server(可变借用)
addr string- 地址(string)
返回
int — 该类型的值
(Server) accept
pub fn (s Server) accept() int
用途
`Server` 上的方法:接受一个连接。失败时 throw。
参数
s Server- 接收者 Server(值接收者)
返回
int — 该类型的值
(&mut Server) close
pub fn (s &mut Server) close() int
用途
`&mut Server` 上的方法:关闭资源。
参数
s &mut Server- 接收者 &mut Server(可变借用)
返回
int — 该类型的值
status_text
pub fn status_text(code int) string
用途
status文本。
参数
code int- 码(int)
返回
string — 结果字符串
(&mut Response) get
pub fn (r &mut Response) get(url string) int
用途
在 `&mut Response` 上调用 `get`。失败时 throw。
参数
r &mut Response- 接收者 &mut Response(可变借用)
url string- url(string)
返回
int — 该类型的值
(&mut Response) post
pub fn (r &mut Response) post(url string, body &string) int
用途
在 `&mut Response` 上调用 `post`。失败时 throw。
参数
r &mut Response- 接收者 &mut Response(可变借用)
url string- url(string)
body &string- 正文(借用,调用方仍持有;类型 &string)
返回
int — 该类型的值
(Response) cookies
pub fn (r Response) cookies() []Cookie
用途
在 `Response` 上调用 `cookies`。
参数
r Response- 接收者 Response(值接收者)
返回
[]Cookie — 切片结果
struct Client
pub struct Client {
pub jar cookiejar.Jar
pub jar_on int
pub timeout_ns int
pub max_redirects int
}
用途
公开结构体 `Client`。
参数
jar cookiejar.Jar- 字段(cookiejar.Jar)
jar_on int- 字段(int)
timeout_ns int- 超时(纳秒)
max_redirects int- 字段(int)
new_client
pub fn new_client() Client
用途
构造 client。
参数
无
返回
Client — 该类型的值
(&mut Client) set_jar
pub fn (c &mut Client) set_jar(j cookiejar.Jar)
用途
`&mut Client` 上的方法:设置 jar。
参数
c &mut Client- 接收者 &mut Client(可变借用)
j cookiejar.Jar- 下标(cookiejar.Jar)
(&mut Client) set_timeout
pub fn (c &mut Client) set_timeout(ns int)
用途
`&mut Client` 上的方法:设置 timeout。
参数
c &mut Client- 接收者 &mut Client(可变借用)
ns int- ns(int)
(&mut Client) do
pub fn (c &mut Client) do(method string, raw_url string, body &string) Response
用途
`&mut Client` 上的方法:执行一次。失败时 throw。
参数
c &mut Client- 接收者 &mut Client(可变借用)
method string- method(string)
raw_url string- raw_url(string)
body &string- 正文(借用,调用方仍持有;类型 &string)
返回
Response — 该类型的值
(&mut Client) get
pub fn (c &mut Client) get(raw_url string) Response
用途
在 `&mut Client` 上调用 `get`。失败时 throw。
参数
c &mut Client- 接收者 &mut Client(可变借用)
raw_url string- raw_url(string)
返回
Response — 该类型的值
(&mut Client) post
pub fn (c &mut Client) post(raw_url string, body &string) Response
用途
在 `&mut Client` 上调用 `post`。失败时 throw。
参数
c &mut Client- 接收者 &mut Client(可变借用)
raw_url string- raw_url(string)
body &string- 正文(借用,调用方仍持有;类型 &string)
返回
Response — 该类型的值
(&mut Request) read
pub fn (req &mut Request) read(fd int) int
用途
`&mut Request` 上的方法:读出最多 max 字节。失败时 throw。
参数
req &mut Request- 接收者 &mut Request(可变借用)
fd int- 文件描述符(int)
返回
int — 该类型的值
write_response
pub fn write_response(fd int, status_code int, body &string) int
用途
写入响应。失败时 throw。
参数
fd int- 文件描述符(int)
status_code int- status_code(int)
body &string- 正文(借用,调用方仍持有;类型 &string)
返回
int — 该类型的值
write
pub fn write(fd int, status_code int, content_type string, body &string) int
用途
写入数据,返回写入的字节数。
参数
fd int- 文件描述符(int)
status_code int- status_code(int)
content_type string- content_type(string)
body &string- 正文(借用,调用方仍持有;类型 &string)
返回
int — 写入的字节数
(&Client) has_idle_connection
pub fn (c &Client) has_idle_connection() bool
用途
`&Client` 上的方法:判断是否具有 idle connection。
参数
c &Client- 接收者 &Client(借用)
返回
bool — 是否成立
struct Context
pub struct Context {
pub fd int
pub req Request
}
用途
公开结构体 `Context`。
参数
fd int- 文件描述符
req Request- 字段(Request)
(Context) method
pub fn (c Context) method() string
用途
在 `Context` 上调用 `method`。
参数
c Context- 接收者 Context(值接收者)
返回
string — 结果字符串
(Context) raw_path
pub fn (c Context) raw_path() string
用途
`Context` 上的方法:rawpath。
参数
c Context- 接收者 Context(值接收者)
返回
string — 结果字符串
(Context) path
pub fn (c Context) path() string
用途
在 `Context` 上调用 `path`。
参数
c Context- 接收者 Context(值接收者)
返回
string — 结果字符串
(Context) query
pub fn (c Context) query() string
用途
`Context` 上的方法:查询串。
参数
c Context- 接收者 Context(值接收者)
返回
string — 结果字符串
(Context) body
pub fn (c Context) body() string
用途
`Context` 上的方法:正文。
参数
c Context- 接收者 Context(值接收者)
返回
string — 结果字符串
(Context) text
pub fn (c Context) text(status int, body string) int
用途
`Context` 上的方法:返回当前文本。失败时 throw。
参数
c Context- 接收者 Context(值接收者)
status int- status(int)
body string- 正文(string)
返回
int — 该类型的值
(Context) html
pub fn (c Context) html(status int, body string) int
用途
`Context` 上的方法:HTML。失败时 throw。
参数
c Context- 接收者 Context(值接收者)
status int- status(int)
body string- 正文(string)
返回
int — 该类型的值
(Context) bytes
pub fn (c Context) bytes(status int, content_type string, body string) int
用途
`Context` 上的方法:返回已缓冲的字节串。失败时 throw。
参数
c Context- 接收者 Context(值接收者)
status int- status(int)
content_type string- content_type(string)
body string- 正文(string)
返回
int — 该类型的值
(Server) serve
pub fn (s Server) serve(handle fn(Context)) int
用途
在 `Server` 上调用 `serve`。
参数
s Server- 接收者 Server(值接收者)
handle fn(Context)- handle(函数值;类型 fn(Context))
返回
int — 该类型的值
listen_and_serve_handler
pub fn listen_and_serve_handler(addr string, handle fn(Context)) int
用途
监听andservehandler。失败时 throw。
参数
addr string- 地址(string)
handle fn(Context)- handle(函数值;类型 fn(Context))
返回
int — 该类型的值
struct Cookie
pub struct Cookie {
pub name string
pub value string
pub path string
pub domain string
pub expires string
pub max_age int
pub secure bool
pub http_only bool
pub same_site string
}
用途
公开结构体 `Cookie`。
参数
name string- 名字
value string- 字段(string)
path string- 文件系统路径
domain string- 字段(string)
expires string- 字段(string)
max_age int- 字段(int)
secure bool- 字段(bool)
http_only bool- 字段(bool)
same_site string- 字段(string)
parse_cookie
pub fn parse_cookie(line &string) []Cookie
用途
解析为 cookie。失败时 throw。
参数
line &string- line(借用,调用方仍持有;类型 &string)
返回
[]Cookie — 切片结果
parse_set_cookie
pub fn parse_set_cookie(line &string) Cookie
用途
解析为 set cookie。失败时 throw。
参数
line &string- line(借用,调用方仍持有;类型 &string)
返回
Cookie — 该类型的值
(Cookie) to_string
pub fn (c Cookie) to_string() string
用途
`Cookie` 上的方法:转成 string。
参数
c Cookie- 接收者 Cookie(值接收者)
返回
string — 结果字符串
(Request) cookies
pub fn (r Request) cookies() []Cookie
用途
在 `Request` 上调用 `cookies`。
参数
r Request- 接收者 Request(值接收者)
返回
[]Cookie — 切片结果
struct FileServer
pub struct FileServer {
pub root string
}
用途
公开结构体 `FileServer`。
参数
root string- 字段(string)
file_server
pub fn file_server(root string) FileServer
用途
文件服务端。
参数
root string- root(string)
返回
FileServer — 该类型的值
listen_and_serve
pub fn listen_and_serve(addr string, root string) int
用途
监听andserve。失败时 throw。
参数
addr string- 地址(string)
root string- root(string)
返回
int — 该类型的值
(FileServer) serve
pub fn (fs FileServer) serve(fd int)
用途
在 `FileServer` 上调用 `serve`。
参数
fs FileServer- 接收者 FileServer(值接收者)
fd int- 文件描述符(int)
serve_file
pub fn serve_file(fd int, name string) int
用途
serve文件。失败时 throw。
参数
fd int- 文件描述符(int)
name string- 名字(string)
返回
int — 该类型的值
(Response) reusable
pub fn (r Response) reusable(method string) bool
用途
在 `Response` 上调用 `reusable`。
参数
r Response- 接收者 Response(值接收者)
method string- method(string)
返回
bool — 是否成立
read_response
pub fn read_response(fd int, method string) Response
用途
读取响应。失败时 throw。
参数
fd int- 文件描述符(int)
method string- method(string)
返回
Response — 该类型的值
detect_content_type
pub fn detect_content_type(data &string) string
用途
detectcontenttype。
参数
data &string- 数据(借用,调用方仍持有;类型 &string)
返回
string — 结果字符串