| ... | ... | @@ -20,51 +20,32 @@ pub const Version = enum { |
| 20 | 20 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4 Initial definition |
| 21 | 21 | /// |
| 22 | 22 | /// https://datatracker.ietf.org/doc/html/rfc5789#section-2 PATCH |
| 23 | | pub const Method = enum(u64) { |
| 24 | | GET = parse("GET"), |
| 25 | | HEAD = parse("HEAD"), |
| 26 | | POST = parse("POST"), |
| 27 | | PUT = parse("PUT"), |
| 28 | | DELETE = parse("DELETE"), |
| 29 | | CONNECT = parse("CONNECT"), |
| 30 | | OPTIONS = parse("OPTIONS"), |
| 31 | | TRACE = parse("TRACE"), |
| 32 | | PATCH = parse("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 | | } |
| 23 | pub const Method = enum { |
| 24 | GET, |
| 25 | HEAD, |
| 26 | POST, |
| 27 | PUT, |
| 28 | DELETE, |
| 29 | CONNECT, |
| 30 | OPTIONS, |
| 31 | TRACE, |
| 32 | PATCH, |
| 50 | 33 | |
| 51 | 34 | /// Returns true if a request of this method is allowed to have a body |
| 52 | 35 | /// Actual behavior from servers may vary and should still be checked |
| 53 | | pub fn requestHasBody(self: Method) bool { |
| 54 | | return switch (self) { |
| 36 | pub fn requestHasBody(m: Method) bool { |
| 37 | return switch (m) { |
| 55 | 38 | .POST, .PUT, .PATCH => true, |
| 56 | 39 | .GET, .HEAD, .DELETE, .CONNECT, .OPTIONS, .TRACE => false, |
| 57 | | else => true, |
| 58 | 40 | }; |
| 59 | 41 | } |
| 60 | 42 | |
| 61 | 43 | /// Returns true if a response to this method is allowed to have a body |
| 62 | 44 | /// Actual behavior from clients may vary and should still be checked |
| 63 | | pub fn responseHasBody(self: Method) bool { |
| 64 | | return switch (self) { |
| 45 | pub fn responseHasBody(m: Method) bool { |
| 46 | return switch (m) { |
| 65 | 47 | .GET, .POST, .DELETE, .CONNECT, .OPTIONS, .PATCH => true, |
| 66 | 48 | .HEAD, .PUT, .TRACE => false, |
| 67 | | else => true, |
| 68 | 49 | }; |
| 69 | 50 | } |
| 70 | 51 | |
| ... | ... | @@ -73,11 +54,10 @@ pub const Method = enum(u64) { |
| 73 | 54 | /// https://developer.mozilla.org/en-US/docs/Glossary/Safe/HTTP |
| 74 | 55 | /// |
| 75 | 56 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1 |
| 76 | | pub fn safe(self: Method) bool { |
| 77 | | return switch (self) { |
| 57 | pub fn safe(m: Method) bool { |
| 58 | return switch (m) { |
| 78 | 59 | .GET, .HEAD, .OPTIONS, .TRACE => true, |
| 79 | 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 | 68 | /// https://developer.mozilla.org/en-US/docs/Glossary/Idempotent |
| 89 | 69 | /// |
| 90 | 70 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.2 |
| 91 | | pub fn idempotent(self: Method) bool { |
| 92 | | return switch (self) { |
| 71 | pub fn idempotent(m: Method) bool { |
| 72 | return switch (m) { |
| 93 | 73 | .GET, .HEAD, .PUT, .DELETE, .OPTIONS, .TRACE => true, |
| 94 | 74 | .CONNECT, .POST, .PATCH => false, |
| 95 | | else => false, |
| 96 | 75 | }; |
| 97 | 76 | } |
| 98 | 77 | |
| ... | ... | @@ -102,11 +81,10 @@ pub const Method = enum(u64) { |
| 102 | 81 | /// https://developer.mozilla.org/en-US/docs/Glossary/cacheable |
| 103 | 82 | /// |
| 104 | 83 | /// https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.3 |
| 105 | | pub fn cacheable(self: Method) bool { |
| 106 | | return switch (self) { |
| 84 | pub fn cacheable(m: Method) bool { |
| 85 | return switch (m) { |
| 107 | 86 | .GET, .HEAD => true, |
| 108 | 87 | .POST, .PUT, .DELETE, .CONNECT, .OPTIONS, .TRACE, .PATCH => false, |
| 109 | | else => false, |
| 110 | 88 | }; |
| 111 | 89 | } |
| 112 | 90 | }; |