Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: add http definitions for Method and Status #10661

Merged
merged 10 commits into from May 11, 2022
Merged

Conversation

nektro
Copy link
Contributor

@nektro nektro commented Jan 21, 2022

starts again the std.http namespace and add definitions for the Method and Status primitives for use in further work and 3rd party libraries

additionally will be used by 910, 2007, 943

Copy link
Contributor

@fogti fogti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch on Status is currently inconsistent

lib/std/http/status.zig Outdated Show resolved Hide resolved
lib/std/http/status.zig Outdated Show resolved Hide resolved
lib/std/http/status.zig Outdated Show resolved Hide resolved
lib/std/http/status.zig Outdated Show resolved Hide resolved
lib/std/http/method.zig Show resolved Hide resolved
@daurnimator daurnimator added the standard library This issue involves writing Zig code for the standard library. label Jan 23, 2022
lib/std/http/method.zig Outdated Show resolved Hide resolved
lib/std/http/method.zig Outdated Show resolved Hide resolved
lib/std/http/method.zig Outdated Show resolved Hide resolved
lib/std/http/method.zig Outdated Show resolved Hide resolved
lib/std/http/method.zig Outdated Show resolved Hide resolved
/// 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling this method could be very misleading (e.g. it might then depend on Vary header)

@fogti
Copy link
Contributor

fogti commented Jan 24, 2022

I think it might make sense to collapse the switch bodies like it's done at other places in the standard library, where all the branches which return true and the branches which return false are grouped; imo this would also make the code more readable, because not as much scrolling is necessary, e.g.

    /// 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,
        };
    }

pub fn responseHasBody(self: Method) bool {
return switch (self) {
.GET, .POST, .DELETE, .CONNECT, .OPTIONS, .PATCH => true,
.HEAD, .PUT, .TRACE => false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why PUT is in this list?

Infact most PUT responses I've seen have a body on them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example of PUT returning a body.

I would still say the stdlib should follow the http standard, but it should not block the ability to get a response body from a PUT request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wouldnt stop a client from receiving a PUT body, but it could raise a warning in the server implementation from sending a body

@andrewrk andrewrk merged commit 3a64693 into ziglang:master May 11, 2022
@nektro nektro deleted the meg-http branch May 12, 2022 00:55
@Inve1951
Copy link
Contributor

Inve1951 commented Nov 2, 2022

Was 499 considered? It's not (yet) standard but commonly used for cancellation. E.g. when the visitor hits ESC in their browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants