v / vlib / net / http / method.v
145 lines · 142 sloc · 3.43 KB · da3112e5453b553ca230d47590e0d3ed6be6478d
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module http
5
6// The methods listed here are all of those on the list available at:
7// https://www.iana.org/assignments/http-methods/http-methods.xhtml
8pub enum Method { // as of 2023-06-20
9 get // Note: get ***should*** remain the first value here, to ensure that http.fetch() by default will use it
10 head
11 post
12 put
13 // uncommon ones:
14 acl
15 baseline_control
16 bind
17 checkin
18 checkout
19 connect
20 copy
21 delete
22 label
23 link
24 lock
25 merge
26 mkactivity
27 mkcalendar
28 mkcol
29 mkredirectref
30 mkworkspace
31 move
32 options
33 orderpatch
34 patch
35 pri
36 propfind
37 proppatch
38 rebind
39 report
40 search
41 trace
42 unbind
43 uncheckout
44 unlink
45 unlock
46 update
47 updateredirectref
48 version_control
49}
50
51// str returns the string representation of the HTTP Method `m`.
52pub fn (m Method) str() string {
53 return match m {
54 .get { 'GET' }
55 .head { 'HEAD' }
56 .post { 'POST' }
57 .put { 'PUT' }
58 // uncommon ones:
59 .acl { 'ACL' }
60 .baseline_control { 'BASELINE-CONTROL' }
61 .bind { 'BIND' }
62 .checkin { 'CHECKIN' }
63 .checkout { 'CHECKOUT' }
64 .connect { 'CONNECT' }
65 .copy { 'COPY' }
66 .delete { 'DELETE' }
67 .label { 'LABEL' }
68 .link { 'LINK' }
69 .lock { 'LOCK' }
70 .merge { 'MERGE' }
71 .mkactivity { 'MKACTIVITY' }
72 .mkcalendar { 'MKCALENDAR' }
73 .mkcol { 'MKCOL' }
74 .mkredirectref { 'MKREDIRECTREF' }
75 .mkworkspace { 'MKWORKSPACE' }
76 .move { 'MOVE' }
77 .options { 'OPTIONS' }
78 .orderpatch { 'ORDERPATCH' }
79 .patch { 'PATCH' }
80 .pri { 'PRI' }
81 .propfind { 'PROPFIND' }
82 .proppatch { 'PROPPATCH' }
83 .rebind { 'REBIND' }
84 .report { 'REPORT' }
85 .search { 'SEARCH' }
86 .trace { 'TRACE' }
87 .unbind { 'UNBIND' }
88 .uncheckout { 'UNCHECKOUT' }
89 .unlink { 'UNLINK' }
90 .unlock { 'UNLOCK' }
91 .update { 'UPDATE' }
92 .updateredirectref { 'UPDATEREDIRECTREF' }
93 .version_control { 'VERSION-CONTROL' }
94 }
95}
96
97// method_from_str returns the corresponding Method enum field
98// given a string `m`, e.g. `'GET'` would return Method.get.
99//
100// Currently, the default value is Method.get for unsupported string value.
101pub fn method_from_str(m string) Method {
102 return match m {
103 'GET' { Method.get }
104 'HEAD' { Method.head }
105 'POST' { Method.post }
106 'PUT' { Method.put }
107 // uncommon ones:
108 'ACL' { Method.acl }
109 'BASELINE-CONTROL' { Method.baseline_control }
110 'BIND' { Method.bind }
111 'CHECKIN' { Method.checkin }
112 'CHECKOUT' { Method.checkout }
113 'CONNECT' { Method.connect }
114 'COPY' { Method.copy }
115 'DELETE' { Method.delete }
116 'LABEL' { Method.label }
117 'LINK' { Method.link }
118 'LOCK' { Method.lock }
119 'MERGE' { Method.merge }
120 'MKACTIVITY' { Method.mkactivity }
121 'MKCALENDAR' { Method.mkcalendar }
122 'MKCOL' { Method.mkcol }
123 'MKREDIRECTREF' { Method.mkredirectref }
124 'MKWORKSPACE' { Method.mkworkspace }
125 'MOVE' { Method.move }
126 'OPTIONS' { Method.options }
127 'ORDERPATCH' { Method.orderpatch }
128 'PATCH' { Method.patch }
129 'PRI' { Method.pri }
130 'PROPFIND' { Method.propfind }
131 'PROPPATCH' { Method.proppatch }
132 'REBIND' { Method.rebind }
133 'REPORT' { Method.report }
134 'SEARCH' { Method.search }
135 'TRACE' { Method.trace }
136 'UNBIND' { Method.unbind }
137 'UNCHECKOUT' { Method.uncheckout }
138 'UNLINK' { Method.unlink }
139 'UNLOCK' { Method.unlock }
140 'UPDATE' { Method.update }
141 'UPDATEREDIRECTREF' { Method.updateredirectref }
142 'VERSION-CONTROL' { Method.version_control }
143 else { Method.get } // always default to .get, it is the safest
144 }
145}
146