| ... | @@ -20,51 +20,32 @@ pub const Version = enum { | ... | @@ -20,51 +20,32 @@ pub const Version = enum { |
| 20 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4 Initial definition | 20 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4 Initial definition |
| 21 | /// | 21 | /// |
| 22 | /// https://datatracker.ietf.org/doc/html/rfc5789#section-2 PATCH | 22 | /// https://datatracker.ietf.org/doc/html/rfc5789#section-2 PATCH |
| 23 | pub const Method = enum(u64) { | 23 | pub const Method = enum { |
| 24 | GET = parse("GET"), | 24 | GET, |
| 25 | HEAD = parse("HEAD"), | 25 | HEAD, |
| 26 | POST = parse("POST"), | 26 | POST, |
| 27 | PUT = parse("PUT"), | 27 | PUT, |
| 28 | DELETE = parse("DELETE"), | 28 | DELETE, |
| 29 | CONNECT = parse("CONNECT"), | 29 | CONNECT, |
| 30 | OPTIONS = parse("OPTIONS"), | 30 | OPTIONS, |
| 31 | TRACE = parse("TRACE"), | 31 | TRACE, |
| 32 | PATCH = parse("PATCH"), | 32 | PATCH, |
| 33 | | | |
| 34 | _, | | |
| 35 | | | |
| 36 | /// Converts `s` into a type that may be used as a `Method` field. | | |
| 37 | /// Asserts that `s` is 24 or fewer bytes. | | |
| 38 | pub fn parse(s: []const u8) u64 { | | |
| 39 | var x: u64 = 0; | | |
| 40 | const len = @min(s.len, @sizeOf(@TypeOf(x))); | | |
| 41 | @memcpy(std.mem.asBytes(&x)[0..len], s[0..len]); | | |
| 42 | return x; | | |
| 43 | } | | |
| 44 | | | |
| 45 | pub fn format(self: Method, w: *Writer) Writer.Error!void { | | |
| 46 | const bytes: []const u8 = @ptrCast(&@intFromEnum(self)); | | |
| 47 | const str = std.mem.sliceTo(bytes, 0); | | |
| 48 | try w.writeAll(str); | | |
| 49 | } | | |
| 50 | | 33 | |
| 51 | /// Returns true if a request of this method is allowed to have a body | 34 | /// Returns true if a request of this method is allowed to have a body |
| 52 | /// Actual behavior from servers may vary and should still be checked | 35 | /// Actual behavior from servers may vary and should still be checked |
| 53 | pub fn requestHasBody(self: Method) bool { | 36 | pub fn requestHasBody(m: Method) bool { |
| 54 | return switch (self) { | 37 | return switch (m) { |
| 55 | .POST, .PUT, .PATCH => true, | 38 | .POST, .PUT, .PATCH => true, |
| 56 | .GET, .HEAD, .DELETE, .CONNECT, .OPTIONS, .TRACE => false, | 39 | .GET, .HEAD, .DELETE, .CONNECT, .OPTIONS, .TRACE => false, |
| 57 | else => true, | | |
| 58 | }; | 40 | }; |
| 59 | } | 41 | } |
| 60 | | 42 | |
| 61 | /// Returns true if a response to this method is allowed to have a body | 43 | /// Returns true if a response to this method is allowed to have a body |
| 62 | /// Actual behavior from clients may vary and should still be checked | 44 | /// Actual behavior from clients may vary and should still be checked |
| 63 | pub fn responseHasBody(self: Method) bool { | 45 | pub fn responseHasBody(m: Method) bool { |
| 64 | return switch (self) { | 46 | return switch (m) { |
| 65 | .GET, .POST, .DELETE, .CONNECT, .OPTIONS, .PATCH => true, | 47 | .GET, .POST, .DELETE, .CONNECT, .OPTIONS, .PATCH => true, |
| 66 | .HEAD, .PUT, .TRACE => false, | 48 | .HEAD, .PUT, .TRACE => false, |
| 67 | else => true, | | |
| 68 | }; | 49 | }; |
| 69 | } | 50 | } |
| 70 | | 51 | |
| ... | @@ -73,11 +54,10 @@ pub const Method = enum(u64) { | ... | @@ -73,11 +54,10 @@ pub const Method = enum(u64) { |
| 73 | /// https://developer.mozilla.org/en-US/docs/Glossary/Safe/HTTP | 54 | /// https://developer.mozilla.org/en-US/docs/Glossary/Safe/HTTP |
| 74 | /// | 55 | /// |
| 75 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1 | 56 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1 |
| 76 | pub fn safe(self: Method) bool { | 57 | pub fn safe(m: Method) bool { |
| 77 | return switch (self) { | 58 | return switch (m) { |
| 78 | .GET, .HEAD, .OPTIONS, .TRACE => true, | 59 | .GET, .HEAD, .OPTIONS, .TRACE => true, |
| 79 | .POST, .PUT, .DELETE, .CONNECT, .PATCH => false, | 60 | .POST, .PUT, .DELETE, .CONNECT, .PATCH => false, |
| 80 | else => false, | | |
| 81 | }; | 61 | }; |
| 82 | } | 62 | } |
| 83 | | 63 | |
| ... | @@ -88,11 +68,10 @@ pub const Method = enum(u64) { | ... | @@ -88,11 +68,10 @@ pub const Method = enum(u64) { |
| 88 | /// https://developer.mozilla.org/en-US/docs/Glossary/Idempotent | 68 | /// https://developer.mozilla.org/en-US/docs/Glossary/Idempotent |
| 89 | /// | 69 | /// |
| 90 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.2 | 70 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.2 |
| 91 | pub fn idempotent(self: Method) bool { | 71 | pub fn idempotent(m: Method) bool { |
| 92 | return switch (self) { | 72 | return switch (m) { |
| 93 | .GET, .HEAD, .PUT, .DELETE, .OPTIONS, .TRACE => true, | 73 | .GET, .HEAD, .PUT, .DELETE, .OPTIONS, .TRACE => true, |
| 94 | .CONNECT, .POST, .PATCH => false, | 74 | .CONNECT, .POST, .PATCH => false, |
| 95 | else => false, | | |
| 96 | }; | 75 | }; |
| 97 | } | 76 | } |
| 98 | | 77 | |
| ... | @@ -102,11 +81,10 @@ pub const Method = enum(u64) { | ... | @@ -102,11 +81,10 @@ pub const Method = enum(u64) { |
| 102 | /// https://developer.mozilla.org/en-US/docs/Glossary/cacheable | 81 | /// https://developer.mozilla.org/en-US/docs/Glossary/cacheable |
| 103 | /// | 82 | /// |
| 104 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.3 | 83 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.3 |
| 105 | pub fn cacheable(self: Method) bool { | 84 | pub fn cacheable(m: Method) bool { |
| 106 | return switch (self) { | 85 | return switch (m) { |
| 107 | .GET, .HEAD => true, | 86 | .GET, .HEAD => true, |
| 108 | .POST, .PUT, .DELETE, .CONNECT, .OPTIONS, .TRACE, .PATCH => false, | 87 | .POST, .PUT, .DELETE, .CONNECT, .OPTIONS, .TRACE, .PATCH => false, |
| 109 | else => false, | | |
| 110 | }; | 88 | }; |
| 111 | } | 89 | } |
| 112 | }; | 90 | }; |