1
Fork 0

net::http::*_header: rename to header_*

This commit is contained in:
Drew DeVault 2023-02-10 14:19:08 +01:00
parent b297d9d3a0
commit 0eef917610
2 changed files with 9 additions and 10 deletions

View file

@ -26,7 +26,7 @@ export fn client_finish(client: *client) void = {
// Adds a default header which is included on all HTTP requests using a given
// [[client]].
export fn client_add_header(client: *client, name: str, val: str) void = {
add_header(&client.default_header, name, val);
header_add(&client.default_header, name, val);
};
fn new_request(client: *client, method: str, target: *uri::uri) request = {
@ -63,7 +63,7 @@ fn new_request(client: *client, method: str, target: *uri::uri) request = {
host = strings::dup(host);
};
defer free(host);
add_header(&req.header, "Host", host);
header_add(&req.header, "Host", host);
return req;
};
@ -89,7 +89,7 @@ fn request_set_content_length(req: *request, body: io::handle) void = {
};
const ln = io::seek(body, 0, io::whence::END)!;
io::seek(body, prev, io::whence::SET)!;
add_header(&req.header, "Content-Length", strconv::ztos(ln: size));
header_add(&req.header, "Content-Length", strconv::ztos(ln: size));
};
fn uri_origin_form(target: *uri::uri) uri::uri = {

View file

@ -6,25 +6,24 @@ use strings;
// List of HTTP headers.
// TODO: [](str, []str)
// s/add_header/header_add/ et al
export type header = [](str, str);
// Adds a given HTTP header, which may be added more than once. The name should
// be canonicalized by the caller.
export fn add_header(head: *header, name: str, val: str) void = {
export fn header_add(head: *header, name: str, val: str) void = {
append(head, (strings::dup(name), strings::dup(val)));
};
// Sets the value of a given HTTP header, removing any previous values. The name
// should be canonicalized by the caller.
export fn set_header(head: *header, name: str, val: str) void = {
del_header(head, name);
add_header(head, name, val);
header_del(head, name);
header_add(head, name, val);
};
// Removes an HTTP header from a list of [[header]]. If multiple headers match
// the given name, all matching headers are removed.
export fn del_header(head: *header, name: str) void = {
export fn header_del(head: *header, name: str) void = {
for (let i = 0z; i < len(head); i += 1) {
if (head[i].0 == name) {
free(head[i].0);
@ -49,7 +48,7 @@ export fn header_dup(head: *header) header = {
let new: header = [];
for (let i = 0z; i < len(head); i += 1) {
const (key, val) = head[i];
add_header(&new, key, val);
header_add(&new, key, val);
};
return new;
};
@ -84,6 +83,6 @@ fn read_header(head: *header, scan: *bufio::scanner) (void | io::error) = {
// TODO: validate field-name
let (name, val) = strings::cut(item, ":");
val = strings::trim(val);
add_header(head, name, val);
header_add(head, name, val);
};
};