Files
marqdo-extension-conformance/marqdo-net/index.mq.md
T

145 lines
4.0 KiB
Markdown

---
import:
utf8: "std:text/utf8"
fmt: "std:format"
io: "std:io/stream"
response: "./response.mq.md"
---
# @exec HTTP client
## start
Open one HTTP(S) request. The returned response owns a stream that the caller
must close. Body text is UTF-8. The request target must already be URL encoded.
`params(url, method = "GET", body = "", accept = "text/plain")`
`if method != "GET" and method != "POST"`
`return response.failure("http.method", "Only GET and POST are supported")`
`end`
`if accept != "text/plain" and accept != "application/json" and accept != "text/event-stream"`
`return response.failure("http.accept", "Unsupported Accept value")`
`end`
`endpoint = target(url)`
`if not endpoint.ok`
`return endpoint`
`end`
`endpoint = endpoint.value`
`if 长度(body) > 16384`
`return response.failure("http.request_limit", "Request text exceeds its byte budget")`
`end`
`size = fmt.字节长度(body)`
`if size > 16384 or (method == "GET" and size > 0)`
`return response.failure("http.request_limit", "Invalid request body length")`
`end`
`head = method + " " + endpoint.path + " HTTP/1.1\r\nHost: " + endpoint.authority + "\r\nConnection: close\r\nAccept: " + accept + "\r\n"`
`if method == "POST"`
`head = head + "Content-Type: application/json\r\nContent-Length: " + 文本(size) + "\r\n"`
`end`
`buf = memory.new(1, 1)`
`length = utf8.编码(head + "\r\n" + body, buf, 0)`
`handle = resource.open(endpoint.kind, endpoint.address, ["read", "write"])`
`sent = io.写全部(handle, buf, 0, length)`
`if not sent.ok`
`resource.close(handle)`
`return sent`
`end`
`result = response.open(handle, buf)`
`if not result.ok`
`resource.close(handle)`
`end`
`return result`
## request
Return status, normalized headers and a bounded UTF-8 response body.
`params(url, method = "GET", body = "", accept = "text/plain")`
`opened = start(url, method, body, accept)`
`if not opened.ok`
`return opened`
`end`
`incoming = opened.value`
`result = response.collect(incoming.stream)`
`resource.close(incoming.stream.cursor.handle)`
`if not result.ok`
`return result`
`end`
`return {ok: true, value: {status: incoming.status, headers: incoming.headers, body: result.value}}`
## target
`params(url)`
`invalid = response.failure("http.url", "Expected HTTP(S), a DNS name or IPv4, and an optional numeric port")`
`if 长度(url) > 2048`
`return invalid`
`end`
`points = text.codepoints(url)`
`separator = 0`
`while separator < 长度(points) and points[separator] != 58`
`separator = separator + 1`
`end`
`if separator + 3 >= 长度(points) or points[separator + 1] != 47 or points[separator + 2] != 47`
`return invalid`
`end`
`scheme = string_part(points, 0, separator)`
`kind = "tcp"`
`port = 80`
`if scheme == "https"`
`kind = "tls"`
`port = 443`
`else`
`if scheme != "http"`
`return invalid`
`end`
`end`
`first = separator + 3`
`last = first`
`while last < 长度(points) and points[last] != 47 and points[last] != 63`
`last = last + 1`
`end`
`authority = string_part(points, first, last)`
`split = first`
`while split < last and points[split] != 58`
`c = points[split]`
`if not ((c >= 48 and c <= 57) or (c >= 65 and c <= 90) or (c >= 97 and c <= 122) or c == 45 or c == 46)`
`return invalid`
`end`
`split = split + 1`
`end`
`if split == first or split - first > 253`
`return invalid`
`end`
`if split < last`
`port = response.number(string_part(points, split + 1, last), 10, 65535)`
`if port == None or port == 0`
`return invalid`
`end`
`end`
`path = "/"`
`if last < 长度(points)`
`i = last`
`while i < 长度(points)`
`if points[i] < 33 or points[i] > 126 or points[i] == 35 or points[i] == 92`
`return invalid`
`end`
`i = i + 1`
`end`
`path = string_part(points, last, 长度(points))`
`if points[last] == 63`
`path = "/" + path`
`end`
`end`
`host = string_part(points, first, split)`
`return {ok: true, value: {kind: kind, address: host + ":" + 文本(port), authority: authority, path: path}}`
## string_part
`params(points, first, last)`
`result = ""`
`while first < last`
`result = result + text.from_codepoint(points[first])`
`first = first + 1`
`end`
`return result`