enumstd.http.Method[src]

Fields

GET = parse("GET")
HEAD = parse("HEAD")
POST = parse("POST")
PUT = parse("PUT")
DELETE = parse("DELETE")
CONNECT = parse("CONNECT")
OPTIONS = parse("OPTIONS")
TRACE = parse("TRACE")
PATCH = parse("PATCH")
_

Functions

Functionparse[src]

pub fn parse(s: []const u8) u64

Converts s into a type that may be used as a Method field. Asserts that s is 24 or fewer bytes.

Parameters

s: []const u8

Source Code

Source code
pub fn parse(s: []const u8) u64 {
    var x: u64 = 0;
    const len = @min(s.len, @sizeOf(@TypeOf(x)));
    @memcpy(std.mem.asBytes(&x)[0..len], s[0..len]);
    return x;
}

Functionwrite[src]

pub fn write(self: Method, w: anytype) !void

Parameters

self: Method

Source Code

Source code
pub fn write(self: Method, w: anytype) !void {
    const bytes = std.mem.asBytes(&@intFromEnum(self));
    const str = std.mem.sliceTo(bytes, 0);
    try w.writeAll(str);
}

FunctionrequestHasBody[src]

pub fn requestHasBody(self: Method) bool

Returns true if a request of this method is allowed to have a body Actual behavior from servers may vary and should still be checked

Parameters

self: Method

Source Code

Source code
pub fn requestHasBody(self: Method) bool {
    return switch (self) {
        .POST, .PUT, .PATCH => true,
        .GET, .HEAD, .DELETE, .CONNECT, .OPTIONS, .TRACE => false,
        else => true,
    };
}

FunctionresponseHasBody[src]

pub fn responseHasBody(self: Method) bool

Returns true if a response to this method is allowed to have a body Actual behavior from clients may vary and should still be checked

Parameters

self: Method

Source Code

Source code
pub fn responseHasBody(self: Method) bool {
    return switch (self) {
        .GET, .POST, .DELETE, .CONNECT, .OPTIONS, .PATCH => true,
        .HEAD, .PUT, .TRACE => false,
        else => true,
    };
}

Functionsafe[src]

pub fn safe(self: Method) bool

Parameters

self: Method

Source Code

Source code
pub fn safe(self: Method) bool {
    return switch (self) {
        .GET, .HEAD, .OPTIONS, .TRACE => true,
        .POST, .PUT, .DELETE, .CONNECT, .PATCH => false,
        else => false,
    };
}

Functionidempotent[src]

pub fn idempotent(self: Method) bool

An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state.

https://developer.mozilla.org/en-US/docs/Glossary/Idempotent

https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.2

Parameters

self: Method

Source Code

Source code
pub fn idempotent(self: Method) bool {
    return switch (self) {
        .GET, .HEAD, .PUT, .DELETE, .OPTIONS, .TRACE => true,
        .CONNECT, .POST, .PATCH => false,
        else => false,
    };
}

Functioncacheable[src]

pub fn cacheable(self: Method) bool

A cacheable response is an HTTP response that can be cached, that is stored to be retrieved and used later, saving a new request to the server.

https://developer.mozilla.org/en-US/docs/Glossary/cacheable

https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.3

Parameters

self: Method

Source Code

Source code
pub fn cacheable(self: Method) bool {
    return switch (self) {
        .GET, .HEAD => true,
        .POST, .PUT, .DELETE, .CONNECT, .OPTIONS, .TRACE, .PATCH => false,
        else => false,
    };
}

Source Code

Source code
pub const Method = enum(u64) {
    GET = parse("GET"),
    HEAD = parse("HEAD"),
    POST = parse("POST"),
    PUT = parse("PUT"),
    DELETE = parse("DELETE"),
    CONNECT = parse("CONNECT"),
    OPTIONS = parse("OPTIONS"),
    TRACE = parse("TRACE"),
    PATCH = parse("PATCH"),

    _,

    /// Converts `s` into a type that may be used as a `Method` field.
    /// Asserts that `s` is 24 or fewer bytes.
    pub fn parse(s: []const u8) u64 {
        var x: u64 = 0;
        const len = @min(s.len, @sizeOf(@TypeOf(x)));
        @memcpy(std.mem.asBytes(&x)[0..len], s[0..len]);
        return x;
    }

    pub fn write(self: Method, w: anytype) !void {
        const bytes = std.mem.asBytes(&@intFromEnum(self));
        const str = std.mem.sliceTo(bytes, 0);
        try w.writeAll(str);
    }

    /// Returns true if a request of this method is allowed to have a body
    /// Actual behavior from servers may vary and should still be checked
    pub fn requestHasBody(self: Method) bool {
        return switch (self) {
            .POST, .PUT, .PATCH => true,
            .GET, .HEAD, .DELETE, .CONNECT, .OPTIONS, .TRACE => false,
            else => true,
        };
    }

    /// Returns true if a response to this method is allowed to have a body
    /// Actual behavior from clients may vary and should still be checked
    pub fn responseHasBody(self: Method) bool {
        return switch (self) {
            .GET, .POST, .DELETE, .CONNECT, .OPTIONS, .PATCH => true,
            .HEAD, .PUT, .TRACE => false,
            else => true,
        };
    }

    /// An HTTP method is safe if it doesn't alter the state of the server.
    ///
    /// https://developer.mozilla.org/en-US/docs/Glossary/Safe/HTTP
    ///
    /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1
    pub fn safe(self: Method) bool {
        return switch (self) {
            .GET, .HEAD, .OPTIONS, .TRACE => true,
            .POST, .PUT, .DELETE, .CONNECT, .PATCH => false,
            else => false,
        };
    }

    /// An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state.
    ///
    /// https://developer.mozilla.org/en-US/docs/Glossary/Idempotent
    ///
    /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.2
    pub fn idempotent(self: Method) bool {
        return switch (self) {
            .GET, .HEAD, .PUT, .DELETE, .OPTIONS, .TRACE => true,
            .CONNECT, .POST, .PATCH => false,
            else => false,
        };
    }

    /// A cacheable response is an HTTP response that can be cached, that is stored to be retrieved and used later, saving a new request to the server.
    ///
    /// https://developer.mozilla.org/en-US/docs/Glossary/cacheable
    ///
    /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.3
    pub fn cacheable(self: Method) bool {
        return switch (self) {
            .GET, .HEAD => true,
            .POST, .PUT, .DELETE, .CONNECT, .OPTIONS, .TRACE, .PATCH => false,
            else => false,
        };
    }
}