#net/http
Xu 0.16.0 API
导入方式: #net/http
公开 API: 23 函数, 68 方法, 10 类型, 0 常量, 0 方法集
提供文本响应
使用 net/http 在请求处理函数中返回 HTTP 文本响应。
此原生示例监听本机回环地址;按 Ctrl-C 停止。在另一终端运行 curl http://127.0.0.1:8080/。
将完整程序保存为 main.xu,在本机运行 xu run main.xu。
@main
#net/http
fn hello(c http.Context) {
c.text(200, "hello\n") >>> (err) {
println(err.msg())
<<
}
}
fn main() int {
http.listen_and_serve_handler("127.0.0.1:8080", hello) >>> (err) {
println(err.msg())
<< 1
}
<< 0
}
预期响应正文:hello,末尾有换行。
服务端在连接接收、缓冲区和超时限额内并发派发完整请求。处理器可以重叠执行,共享可变状态须同步,并显式传递请求所有权。使用 server_options() 获取默认值,通过 serve_with_options 配置限额;响应短写会重试,空闲或不完整请求不会无限占用处理器。
示例代码 · API 声明
示例代码
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
12_file_server/main.xu
所需 SDK:Xu 0.8.13-dev.7 · 完整工程 · 下载源码
@main
#net/http
// Go:
// http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))
// Xu:
// http.listen_and_serve("127.0.0.1:8080", ".")
//
// 参数:要对外的目录。默认本示例的 www/(从仓库根运行)。
fn main() int {
$root := "examples/12_file_server/www"
av := args()
if av.len >= 2 {
root = av[1] + ""
}
println("FileServer " + root)
println("http://127.0.0.1:8080")
http.listen_and_serve("127.0.0.1:8080", root) >>> (err) {
println(err.msg())
<< 1
}
<< 0
}
13_http_context/main.xu
所需 SDK:Xu 0.8.13-dev.7 · 完整工程 · 下载源码
@main
#net/http
// 写法 C — 请求上下文(Go Handler / HandlerFunc)。
//
// Go:
// func hello(w http.ResponseWriter, r *http.Request) {
// fmt.Fprint(w, "hello")
// }
// http.ListenAndServe(":8080", http.HandlerFunc(hello))
//
// Xu: w 和 r 收成一个 Context。
fn app(c http.Context) {
p := c.path()
if p == "/" || p == "/index.html" {
page := "<!doctype html><html><head><meta charset=\"utf-8\"><title>Xu</title></head>" +
"<body><h1>Context</h1>" +
"<p><a href=\"/hello\">/hello</a> · <a href=\"/health\">/health</a></p>" +
"<p>POST /echo</p></body></html>"
c.html(200, page) >>> (err) { << }
<<
}
if p == "/hello" {
c.text(200, "hello, xu\n") >>> (err) { << }
<<
}
if p == "/health" {
c.text(200, "ok\n") >>> (err) { << }
<<
}
if p == "/echo" {
m := c.method()
if m != "POST" && m != "PUT" {
c.text(405, "method not allowed\n") >>> (err) { << }
<<
}
b := c.body()
c.text(200, b) >>> (err) { << }
<<
}
miss := "not found: " + p + "\n"
c.text(404, miss) >>> (err) { << }
}
fn main() int {
println("http://127.0.0.1:18081")
http.listen_and_serve_handler("127.0.0.1:18081", app) >>> (err) {
println(err.msg())
<< 1
}
<< 0
}
16_ui_webview/xu_design_system/server.xu
所需 SDK:Xu 0.10.11 · 完整工程 · 下载源码
@main
#net/http
// Xu Design System & VI Specification 官方本地 HTTP 服务
// 运行指令:
// ./xu_design_server.xin [--addr 127.0.0.1:8088] [--root <www-dir>]
// 位置参数仍兼容旧形式:<port> <www-dir>。
fn main() int {
$addr := "127.0.0.1:8088"
$root := "examples/16_ui_webview/xu_design_system/www"
av := args()
$i := 1
while i < av.len {
arg := av[i] + ""
if arg == "--help" || arg == "-h" {
println("Xu Design System HTTP server")
println("usage: xu_design_server [--addr HOST:PORT] [--root DIR]")
println("legacy: xu_design_server PORT DIR")
<< 0
}
if arg == "--addr" {
if i + 1 >= av.len {
println("missing value for --addr")
<< 2
}
addr = av[i + 1] + ""
i = i + 2
->
}
if arg == "--root" {
if i + 1 >= av.len {
println("missing value for --root")
<< 2
}
root = av[i + 1] + ""
i = i + 2
->
}
// Preserve the original positional interface: port, then root.
if i == 1 {
addr = "127.0.0.1:" + arg
} else if i == 2 {
root = arg
} else {
println("unknown argument: " + arg)
<< 2
}
i = i + 1
}
println("==================================================================")
println(" __ __ _ _ ")
println(" \\ \\/ / | | | | ⚡ Xu Official HTTP Server (Design System)")
println(" \\ / | | | | 🔥 Rock-Solid Ownership · Supersonic AOT")
println(" / \\ | |_| | 🌐 Serving: " + root)
println(" /_/\\_\\ \\___/ 🔗 Local URL: http://" + addr + "/")
println("==================================================================")
println("Press Ctrl+C to stop server.\n")
http.listen_and_serve(addr, root) >>> (err) {
println("Server error: " + err.msg())
<< 1
}
<< 0
}
更多相关示例
-
11_http_server/main.xu
所需 SDK:Xu 0.8.13-dev.7 · 完整工程 · 下载源码
@main
#net
#net/http
// 自己 listen / accept / 按路径处理(Go HandleFunc 那种)。
// GET / HTML
// GET /hello text
// GET /health ok
// POST /echo 回显 body
// 静态目录见 12file_server;Handler 上下文见 13http_context。
fn path_only(p string) string {
$i := 0
while i < p.len {
if (p[i] & 255) == 63 {
<< p[0:i]
}
i = i + 1
}
<< p + ""
}
fn write_http(fd int, status int, ctype string, body string) int {
$reason := "OK"
if status == 404 {
reason = "Not Found"
}
if status == 400 {
reason = "Bad Request"
}
if status == 405 {
reason = "Method Not Allowed"
}
$out := "HTTP/1.1 " + str(status) + " " + reason + "\r\n"
out = out + "Content-Type: " + ctype + "\r\n"
out = out + "Content-Length: " + str(body.len) + "\r\n"
out = out + "Connection: close\r\n"
out = out + "\r\n"
out = out + body
<< net.conn_write(fd, &out) >>> (err) { >> err }
}
fn index_page() string {
<< "<!doctype html><html><head><meta charset=\"utf-8\"><title>Xu HTTP</title>" +
"<style>body{font-family:ui-sans-serif,system-ui,sans-serif;max-width:40rem;" +
"margin:3rem auto;padding:0 1.5rem;background:#0f1419;color:#e7ecf1}" +
"h1{font-weight:650;letter-spacing:-.03em}a{color:#7dd3fc}" +
"code{background:#1b2430;padding:.15rem .4rem;border-radius:4px}" +
"ul{line-height:1.8}</style></head><body>" +
"<h1>Xu HTTP</h1>" +
"<p>自己 <code>listen</code> / <code>accept</code> / 按 path 分支。</p>" +
"<ul>" +
"<li><a href=\"/hello\">/hello</a></li>" +
"<li><a href=\"/health\">/health</a></li>" +
"<li>POST <code>/echo</code></li>" +
"</ul></body></html>"
}
fn handle(fd int) {
$req := http.new_request()
req.read(fd) >>> (err) {
msg := err.msg()
write_http(fd, 400, "text/plain; charset=utf-8", msg) >>> (e2) { << }
net.conn_close(fd) >>> (e3) { << }
<<
}
method := req.method()
raw_path := req.path()
path := path_only(raw_path)
if path == "/" || path == "/index.html" {
page := index_page()
write_http(fd, 200, "text/html; charset=utf-8", page) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
if path == "/hello" {
body := "hello, xu\n"
write_http(fd, 200, "text/plain; charset=utf-8", body) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
if path == "/health" {
body := "ok\n"
write_http(fd, 200, "text/plain; charset=utf-8", body) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
if path == "/echo" {
if method != "POST" && method != "PUT" {
body := "method not allowed\n"
write_http(fd, 405, "text/plain; charset=utf-8", body) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
echo := req.body()
write_http(fd, 200, "text/plain; charset=utf-8", echo) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
miss := "not found: " + path + "\n"
write_http(fd, 404, "text/plain; charset=utf-8", miss) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
}
fn main() int {
$s := http.new_server()
s.listen("127.0.0.1:18080") >>> (err) {
println(err.msg())
<< 1
}
println("http://127.0.0.1:18080")
$running := true
while running {
fd := s.accept() >>> (err) {
->
}
// The deployable example uses the portable, deterministic path on all
// hosts. The stackful `xu` scheduler has dedicated stress fixtures,
// but it is not yet a production request-execution primitive; keeping
// request handling sequential also gives local and server builds the
// same lifecycle contract. Put TLS, limits and concurrency in the
// reverse proxy until that runtime gate is closed.
handle(fd)
}
<< 0
}
API 声明
struct Response
pub struct Response {
pub status_code int
pub status string
pub headers string
pub body string
pub sticky string
}
用途
Http 服务端响应结果,包含状态码、响应头与正文数据。
公开字段
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 code 操作。
参数
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
pub remote_addr string
}
用途
承载 method、path、headers、body、sticky 等属性的结构体。
公开字段
method string- 字段(string)
path string- 文件系统路径
headers string- 字段(string)
body string- 正文
sticky string- 字段(string)
remote_addr 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 上的操作:执行 user agent 操作。
参数
r Request- 本次调用使用的
Request 实例;按值接收。
返回值
string — 结果字符串
(Request) host
pub fn (r Request) host() string
用途
Request 上的操作:主机。
参数
r Request- 本次调用使用的
Request 实例;按值接收。
返回值
string — 结果字符串
(Request) remote_addr
pub fn (r Request) remote_addr() string
用途
Request 上的操作:执行 remote addr 操作。
参数
r Request- 本次调用使用的
Request 实例;按值接收。
返回值
string — 结果字符串
(Request) query_value
pub fn (r Request) query_value(key string) string
用途
Request 上的操作:查询串值。
参数
r Request- 本次调用使用的
Request 实例;按值接收。
key string- 键(string)
返回值
string — 结果字符串
(Request) form_value
pub fn (r Request) form_value(key string) string
用途
Request 上的操作:执行 form value 操作。
参数
r Request- 本次调用使用的
Request 实例;按值接收。
key string- 键(string)
返回值
string — 结果字符串
struct Server
pub struct Server {
pub lfd int
pub laddr string
}
用途
承载 lfd、laddr 等属性的结构体。
公开字段
lfd int- 字段(int)
laddr string- 字段(string)
new_server
pub fn new_server() Server
用途
构造 server。
参数
无需传入参数。
返回值
Server — 该类型的值
查看用法示例
(Server) clone
pub fn (s Server) clone() Server
用途
Server 上的操作:拷贝一份独立的 string。
参数
s Server- 本次调用使用的
Server 实例;按值接收。
返回值
Server — 该类型的值
($&Server) listen
pub fn (s $&Server) listen(addr string) int
用途
$&Server 上的操作:开始监听。失败时抛出错误,调用方需处理或继续传播。
参数
s $&Server- 本次调用使用的
Server 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
addr string- 地址(string)
返回值
int — 该类型的值
(Server) addr
pub fn (s Server) addr() string
用途
Server 上的操作:地址。
参数
s Server- 本次调用使用的
Server 实例;按值接收。
返回值
string — 结果字符串
(Server) accept
pub fn (s Server) accept() int
用途
Server 上的操作:接受一个连接。失败时抛出错误,调用方需处理或继续传播。
参数
s Server- 本次调用使用的
Server 实例;按值接收。
返回值
int — 该类型的值
($&Server) close
pub fn (s $&Server) close() int
用途
$&Server 上的操作:关闭资源。
参数
s $&Server- 本次调用使用的
Server 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
int — 该类型的值
($&Server) shutdown
pub fn (s $&Server) shutdown() int
用途
在 $&Server 上执行 shutdown 操作。
参数
s $&Server- 本次调用使用的
Server 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
int — 该类型的值
status_text
pub fn status_text(code int) string
用途
执行 status text 操作。
参数
code int- 码(int)
返回值
string — 结果字符串
($&Response) get
pub fn (r $&Response) get(url string) int
用途
在 $&Response 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。
参数
r $&Response- 本次调用使用的
Response 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
url string- url(string)
返回值
int — 该类型的值
($&Response) post
pub fn (r $&Response) post(url string, body &string) int
用途
在 $&Response 上执行 post 操作。失败时抛出错误,调用方需处理或继续传播。
参数
r $&Response- 本次调用使用的
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 insecure_skip_verify bool
pub timeout_ns int
pub max_redirects int
pub max_response_bytes int
}
用途
Http 客户端会话实例,维护与服务端的连接并提供操作接口。
公开字段
jar cookiejar.Jar- 字段(cookiejar.Jar)
jar_on int- 字段(int)
insecure_skip_verify bool- 字段(bool)
timeout_ns int- 超时(纳秒)
max_redirects int- 字段(int)
max_response_bytes int- 字段(int)
new_client
pub fn new_client() Client
用途
构造 client。
参数
无需传入参数。
返回值
Client — 该类型的值
($&Client) set_jar
pub fn (c $&Client) set_jar(j cookiejar.Jar)
用途
$&Client 上的操作:设置 jar。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
j cookiejar.Jar- 下标(cookiejar.Jar)
返回值
不返回值。
($&Client) set_timeout
pub fn (c $&Client) set_timeout(ns int)
用途
$&Client 上的操作:设置 timeout。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
ns int- ns(int)
返回值
不返回值。
($&Client) do
pub fn (c $&Client) do(method string, raw_url string, body &string) Response
用途
$&Client 上的操作:执行一次。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
method string- method(string)
raw_url string- raw_url(string)
body &string- 正文(只读借用,调用方仍持有原值;类型 &string)
返回值
Response — 该类型的值
($&Client) get
pub fn (c $&Client) get(raw_url string) Response
用途
在 $&Client 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
raw_url string- raw_url(string)
返回值
Response — 该类型的值
($&Client) post
pub fn (c $&Client) post(raw_url string, body &string) Response
用途
在 $&Client 上执行 post 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
raw_url string- raw_url(string)
body &string- 正文(只读借用,调用方仍持有原值;类型 &string)
返回值
Response — 该类型的值
($&Client) post_form
pub fn (c $&Client) post_form(raw_url string, data &url.Query) Response
用途
$&Client 上的操作:执行 post form 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
raw_url string- raw_url(string)
data &url.Query- 数据(只读借用,调用方仍持有原值;类型 &url.Query)
返回值
Response — 该类型的值
($&Request) read
pub fn (req $&Request) read(fd int) int
用途
$&Request 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
参数
req $&Request- 本次调用使用的
Request 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
fd int- 文件描述符(int)
返回值
int — 该类型的值
($&Request) read_timeout
pub fn (req $&Request) read_timeout(fd int, timeout_ns int) int
用途
$&Request 上的操作:读取超时。失败时抛出错误,调用方需处理或继续传播。
参数
req $&Request- 本次调用使用的
Request 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
fd int- 文件描述符(int)
timeout_ns int- 超时(纳秒)(int)
返回值
int — 该类型的值
write_response
pub fn write_response(fd int, status_code int, body &string) int
用途
写入响应。失败时抛出错误,调用方需处理或继续传播。
参数
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 — 是否成立
($&Client) close_idle_connections
pub fn (c $&Client) close_idle_connections()
用途
$&Client 上的操作:执行 close idle connections 操作。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
struct Context
pub struct Context {
pub fd int
pub req Request
}
用途
承载 fd、req 等属性的结构体。
公开字段
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 上的操作:执行 raw path 操作。
参数
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) remote_addr
pub fn (c Context) remote_addr() string
用途
Context 上的操作:执行 remote addr 操作。
参数
c Context- 本次调用使用的
Context 实例;按值接收。
返回值
string — 结果字符串
(Context) query_value
pub fn (c Context) query_value(key string) string
用途
Context 上的操作:查询串值。
参数
c Context- 本次调用使用的
Context 实例;按值接收。
key string- 键(string)
返回值
string — 结果字符串
(Context) form_value
pub fn (c Context) form_value(key string) string
用途
Context 上的操作:执行 form value 操作。
参数
c Context- 本次调用使用的
Context 实例;按值接收。
key string- 键(string)
返回值
string — 结果字符串
(Context) body
pub fn (c Context) body() string
用途
Context 上的操作:正文。
参数
c Context- 本次调用使用的
Context 实例;按值接收。
返回值
string — 结果字符串
(Context) persistent
pub fn (c Context) persistent() bool
用途
在 Context 上执行 persistent 操作。
参数
c Context- 本次调用使用的
Context 实例;按值接收。
返回值
bool — 是否成立
(Context) hijack
pub fn (c Context) hijack() int
用途
在 Context 上执行 hijack 操作。
参数
c Context- 本次调用使用的
Context 实例;按值接收。
返回值
int — 该类型的值
(Context) text
pub fn (c Context) text(status int, body string) int
用途
Context 上的操作:返回当前文本。失败时抛出错误,调用方需处理或继续传播。
参数
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。失败时抛出错误,调用方需处理或继续传播。
参数
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 上的操作:返回已缓冲的字节串。失败时抛出错误,调用方需处理或继续传播。
参数
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
用途
执行 listen and serve handler 操作。失败时抛出错误,调用方需处理或继续传播。
参数
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
}
用途
承载 name、value、path、domain、expires 等属性的结构体。
公开字段
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。失败时抛出错误,调用方需处理或继续传播。
参数
line &string- line(只读借用,调用方仍持有原值;类型 &string)
返回值
[]Cookie — 切片结果
parse_set_cookie
pub fn parse_set_cookie(line &string) Cookie
用途
解析为 set cookie。失败时抛出错误,调用方需处理或继续传播。
参数
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
}
用途
承载 root 等属性的结构体。
公开字段
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
用途
执行 listen and serve 操作。失败时抛出错误,调用方需处理或继续传播。
参数
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)
返回值
不返回值。
(FileServer) serve_context
pub fn (fs FileServer) serve_context(c Context)
用途
FileServer 上的操作:执行 serve context 操作。
参数
fs FileServer- 本次调用使用的
FileServer 实例;按值接收。
c Context- 字节或字符码(Context)
返回值
不返回值。
serve_file
pub fn serve_file(fd int, name string) int
用途
执行 serve file 操作。失败时抛出错误,调用方需处理或继续传播。
参数
fd int- 文件描述符(int)
name string- 名字(string)
返回值
int — 该类型的值
struct ServeMux
pub struct ServeMux {
pub patterns []string
pub handlers []fn(Context)
}
用途
承载 patterns、handlers 等属性的结构体。
公开字段
patterns []string- 字段([]string)
handlers []fn(Context)- 字段([]fn(Context))
new_serve_mux
pub fn new_serve_mux() ServeMux
用途
构造 serve mux。
参数
无需传入参数。
返回值
ServeMux — 该类型的值
($&ServeMux) handle_func
pub fn (m $&ServeMux) handle_func(pattern string, handler fn(Context))
用途
$&ServeMux 上的操作:执行 handle func 操作。
参数
m $&ServeMux- 本次调用使用的
ServeMux 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
pattern string- 匹配模式(string)
handler fn(Context)- handler(函数值;类型 fn(Context))
返回值
不返回值。
(&ServeMux) serve_http
pub fn (m &ServeMux) serve_http(c Context)
用途
&ServeMux 上的操作:执行 serve http 操作。
参数
m &ServeMux- 本次调用使用的
ServeMux 实例;以只读借用传入,调用方仍持有实例。
c Context- 字节或字符码(Context)
返回值
不返回值。
($&Client) set_download_progress
pub fn (c $&Client) set_download_progress(report fn(int, int))
用途
$&Client 上的操作:设置 download progress。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
report fn(int, int)- report(函数值;类型 fn(int, 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
用途
读取响应。失败时抛出错误,调用方需处理或继续传播。
参数
fd int- 文件描述符(int)
method string- method(string)
返回值
Response — 该类型的值
struct ServerOptions
pub struct ServerOptions {
pub read_timeout_ns int
pub write_timeout_ns int
pub max_connections int
pub max_header_bytes int
pub max_body_bytes int
pub max_buffer_bytes int
pub max_active_handlers int
pub workers int
pub sync_handlers bool
}
用途
Http 配置参数与选项集合。
公开字段
read_timeout_ns int- 字段(int)
write_timeout_ns int- 字段(int)
max_connections int- 字段(int)
max_header_bytes int- 字段(int)
max_body_bytes int- 字段(int)
max_buffer_bytes int- 字段(int)
max_active_handlers int- 字段(int)
workers int- 字段(int)
sync_handlers bool- 字段(bool)
server_options
pub fn server_options() ServerOptions
用途
执行 server options 操作。
参数
无需传入参数。
返回值
ServerOptions — 该类型的值
serve_listener
pub fn serve_listener(lfd int, handle fn(Context)) int
用途
执行 serve listener 操作。
参数
lfd int- lfd(int)
handle fn(Context)- handle(函数值;类型 fn(Context))
返回值
int — 该类型的值
(&Server) serve_with_options
pub fn (s &Server) serve_with_options(handle fn(Context), options ServerOptions) int
用途
&Server 上的操作:执行 serve with options 操作。失败时抛出错误,调用方需处理或继续传播。
参数
s &Server- 本次调用使用的
Server 实例;以只读借用传入,调用方仍持有实例。
handle fn(Context)- handle(函数值;类型 fn(Context))
options ServerOptions- options(ServerOptions)
返回值
int — 该类型的值
serve_listener_with_options
pub fn serve_listener_with_options(lfd int, handle fn(Context), options ServerOptions) int
用途
执行 serve listener with options 操作。失败时抛出错误,调用方需处理或继续传播。
参数
lfd int- lfd(int)
handle fn(Context)- handle(函数值;类型 fn(Context))
options ServerOptions- options(ServerOptions)
返回值
int — 该类型的值
detect_content_type
pub fn detect_content_type(data &string) string
用途
通过嗅探输入数据的前 512 字节推断并返回 MIME Content-Type。
参数
data &string- 数据(只读借用,调用方仍持有原值;类型 &string)
返回值
string — 结果字符串