0
# HTTP Methods and Status
1
2
Standard HTTP method and status code handling with safety and idempotency checks, efficient parsing, caching, and comprehensive status code coverage.
3
4
## Capabilities
5
6
### HttpMethod Enumeration
7
8
Complete enumeration of HTTP methods with efficiency optimizations and semantic checks.
9
10
```java { .api }
11
/**
12
* Enumeration of HTTP methods with all IANA-registered methods
13
*/
14
enum HttpMethod {
15
// IANA registered HTTP methods (from HttpMethod.java source)
16
ACL, BASELINE_CONTROL, BIND, CHECKIN, CHECKOUT, CONNECT, COPY, DELETE,
17
GET, HEAD, LABEL, LINK, LOCK, MERGE, MKACTIVITY, MKCALENDAR, MKCOL,
18
MKREDIRECTREF, MKWORKSPACE, MOVE, OPTIONS, ORDERPATCH, PATCH, POST,
19
PRI, PROPFIND, PROPPATCH, PUT, REBIND, REPORT, SEARCH, TRACE,
20
UNBIND, UNCHECKOUT, UNLINK, UNLOCK, UPDATE, UPDATEREDIRECTREF,
21
VERSION_CONTROL,
22
23
// Other methods
24
PROXY;
25
26
/** Get method as byte array for efficient processing */
27
byte[] getBytes();
28
29
/** Check if method matches string (case sensitive) */
30
boolean is(String s);
31
32
/** Check if method is safe (read-only, no side effects) */
33
boolean isSafe();
34
35
/** Check if method is idempotent (can be repeated safely) */
36
boolean isIdempotent();
37
38
/** Get method as ByteBuffer for network operations */
39
ByteBuffer asBuffer();
40
41
/** Get method as string */
42
String asString();
43
44
/** Parse method from string, null if not found */
45
static HttpMethod fromString(String method);
46
47
/** Optimized parsing for GET method from buffer */
48
static HttpMethod lookAheadGet(ByteBuffer buffer);
49
50
/** Optimized parsing with pre-computed integer representation */
51
static HttpMethod lookAheadGet(ByteBuffer buffer, int lookAhead);
52
53
/** Case-sensitive method cache for fast lookup */
54
static final Index<HttpMethod> CACHE;
55
56
/** Case-insensitive method cache */
57
static final Index<HttpMethod> INSENSITIVE_CACHE;
58
59
/** Look-ahead cache for method parsing with trailing space */
60
static final Index<HttpMethod> LOOK_AHEAD;
61
62
/** Pre-computed integer representations for efficiency */
63
static final int GET_AS_INT;
64
static final int POST_AS_INT;
65
static final int PUT_AS_INT;
66
static final int HEAD_AS_INT;
67
static final int PRI_AS_INT;
68
static final int ACL_AS_INT;
69
}
70
```
71
72
### HttpStatus Class
73
74
Comprehensive HTTP status code handling with utilities and nested Code enumeration.
75
76
```java { .api }
77
/**
78
* HTTP status codes and utilities
79
*/
80
class HttpStatus {
81
// 1xx Informational responses
82
static final int CONTINUE_100 = 100;
83
static final int SWITCHING_PROTOCOLS_101 = 101;
84
static final int PROCESSING_102 = 102;
85
static final int EARLY_HINTS_103 = 103;
86
87
// 2xx Success responses
88
static final int OK_200 = 200;
89
static final int CREATED_201 = 201;
90
static final int ACCEPTED_202 = 202;
91
static final int NON_AUTHORITATIVE_INFORMATION_203 = 203;
92
static final int NO_CONTENT_204 = 204;
93
static final int RESET_CONTENT_205 = 205;
94
static final int PARTIAL_CONTENT_206 = 206;
95
static final int MULTI_STATUS_207 = 207;
96
97
// 3xx Redirection responses
98
static final int MULTIPLE_CHOICES_300 = 300;
99
static final int MOVED_PERMANENTLY_301 = 301;
100
static final int MOVED_TEMPORARILY_302 = 302;
101
static final int FOUND_302 = 302;
102
static final int SEE_OTHER_303 = 303;
103
static final int NOT_MODIFIED_304 = 304;
104
static final int USE_PROXY_305 = 305;
105
static final int TEMPORARY_REDIRECT_307 = 307;
106
static final int PERMANENT_REDIRECT_308 = 308;
107
108
// 4xx Client error responses
109
static final int BAD_REQUEST_400 = 400;
110
static final int UNAUTHORIZED_401 = 401;
111
static final int PAYMENT_REQUIRED_402 = 402;
112
static final int FORBIDDEN_403 = 403;
113
static final int NOT_FOUND_404 = 404;
114
static final int METHOD_NOT_ALLOWED_405 = 405;
115
static final int NOT_ACCEPTABLE_406 = 406;
116
static final int PROXY_AUTHENTICATION_REQUIRED_407 = 407;
117
static final int REQUEST_TIMEOUT_408 = 408;
118
static final int CONFLICT_409 = 409;
119
static final int GONE_410 = 410;
120
static final int LENGTH_REQUIRED_411 = 411;
121
static final int PRECONDITION_FAILED_412 = 412;
122
static final int PAYLOAD_TOO_LARGE_413 = 413;
123
static final int URI_TOO_LONG_414 = 414;
124
static final int UNSUPPORTED_MEDIA_TYPE_415 = 415;
125
static final int RANGE_NOT_SATISFIABLE_416 = 416;
126
static final int EXPECTATION_FAILED_417 = 417;
127
static final int IM_A_TEAPOT_418 = 418;
128
static final int ENHANCE_YOUR_CALM_420 = 420;
129
static final int MISDIRECTED_REQUEST_421 = 421;
130
static final int UNPROCESSABLE_ENTITY_422 = 422;
131
static final int LOCKED_423 = 423;
132
static final int FAILED_DEPENDENCY_424 = 424;
133
static final int UPGRADE_REQUIRED_426 = 426;
134
static final int PRECONDITION_REQUIRED_428 = 428;
135
static final int TOO_MANY_REQUESTS_429 = 429;
136
static final int REQUEST_HEADER_FIELDS_TOO_LARGE_431 = 431;
137
static final int UNAVAILABLE_FOR_LEGAL_REASONS_451 = 451;
138
139
// 5xx Server error responses
140
static final int INTERNAL_SERVER_ERROR_500 = 500;
141
static final int NOT_IMPLEMENTED_501 = 501;
142
static final int BAD_GATEWAY_502 = 502;
143
static final int SERVICE_UNAVAILABLE_503 = 503;
144
static final int GATEWAY_TIMEOUT_504 = 504;
145
static final int HTTP_VERSION_NOT_SUPPORTED_505 = 505;
146
static final int INSUFFICIENT_STORAGE_507 = 507;
147
static final int LOOP_DETECTED_508 = 508;
148
static final int NOT_EXTENDED_510 = 510;
149
static final int NETWORK_AUTHENTICATION_REQUIRED_511 = 511;
150
151
/** Maximum status code value */
152
static final int MAX_CODE = 511;
153
154
/** Get Code enum for status code */
155
static Code getCode(int code);
156
157
/** Get standard message for status code */
158
static String getMessage(int code);
159
160
/** Check if status should have no response body */
161
static boolean hasNoBody(int status);
162
163
/** Check if status is informational (1xx) */
164
static boolean isInformational(int code);
165
166
/** Check if status is success (2xx) */
167
static boolean isSuccess(int code);
168
169
/** Check if status is redirection (3xx) */
170
static boolean isRedirection(int code);
171
172
/** Check if status is client error (4xx) */
173
static boolean isClientError(int code);
174
175
/** Check if status is server error (5xx) */
176
static boolean isServerError(int code);
177
178
/** Check if status is interim response (1xx but not 101) */
179
static boolean isInterim(int code);
180
181
/**
182
* HTTP status code enumeration with semantic methods
183
*/
184
enum Code {
185
CONTINUE(100, "Continue"),
186
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
187
PROCESSING(102, "Processing"),
188
EARLY_HINT(103, "Early Hint"), // @Deprecated(forRemoval = true)
189
EARLY_HINTS(103, "Early Hints"),
190
191
OK(200, "OK"),
192
CREATED(201, "Created"),
193
ACCEPTED(202, "Accepted"),
194
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
195
NO_CONTENT(204, "No Content"),
196
RESET_CONTENT(205, "Reset Content"),
197
PARTIAL_CONTENT(206, "Partial Content"),
198
MULTI_STATUS(207, "Multi-Status"),
199
200
MULTIPLE_CHOICES(300, "Multiple Choices"),
201
MOVED_PERMANENTLY(301, "Moved Permanently"),
202
MOVED_TEMPORARILY(302, "Moved Temporarily"),
203
FOUND(302, "Found"),
204
SEE_OTHER(303, "See Other"),
205
NOT_MODIFIED(304, "Not Modified"),
206
USE_PROXY(305, "Use Proxy"),
207
TEMPORARY_REDIRECT(307, "Temporary Redirect"),
208
PERMANENT_REDIRECT(308, "Permanent Redirect"),
209
210
BAD_REQUEST(400, "Bad Request"),
211
UNAUTHORIZED(401, "Unauthorized"),
212
PAYMENT_REQUIRED(402, "Payment Required"),
213
FORBIDDEN(403, "Forbidden"),
214
NOT_FOUND(404, "Not Found"),
215
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
216
NOT_ACCEPTABLE(406, "Not Acceptable"),
217
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
218
REQUEST_TIMEOUT(408, "Request Timeout"),
219
CONFLICT(409, "Conflict"),
220
GONE(410, "Gone"),
221
LENGTH_REQUIRED(411, "Length Required"),
222
PRECONDITION_FAILED(412, "Precondition Failed"),
223
PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
224
URI_TOO_LONG(414, "URI Too Long"),
225
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
226
RANGE_NOT_SATISFIABLE(416, "Range Not Satisfiable"),
227
EXPECTATION_FAILED(417, "Expectation Failed"),
228
IM_A_TEAPOT(418, "I'm a Teapot"),
229
ENHANCE_YOUR_CALM(420, "Enhance your Calm"),
230
MISDIRECTED_REQUEST(421, "Misdirected Request"),
231
UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"),
232
LOCKED(423, "Locked"),
233
FAILED_DEPENDENCY(424, "Failed Dependency"),
234
UPGRADE_REQUIRED(426, "Upgrade Required"),
235
PRECONDITION_REQUIRED(428, "Precondition Required"),
236
TOO_MANY_REQUESTS(429, "Too Many Requests"),
237
REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"),
238
UNAVAILABLE_FOR_LEGAL_REASONS(451, "Unavailable for Legal Reason"),
239
240
INTERNAL_SERVER_ERROR(500, "Server Error"),
241
NOT_IMPLEMENTED(501, "Not Implemented"),
242
BAD_GATEWAY(502, "Bad Gateway"),
243
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
244
GATEWAY_TIMEOUT(504, "Gateway Timeout"),
245
HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported"),
246
INSUFFICIENT_STORAGE(507, "Insufficient Storage"),
247
LOOP_DETECTED(508, "Loop Detected"),
248
NOT_EXTENDED(510, "Not Extended"),
249
NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required");
250
251
/** Get numeric status code */
252
int getCode();
253
254
/** Get standard status message */
255
String getMessage();
256
257
/** Check if status is informational (1xx) */
258
boolean isInformational();
259
260
/** Check if status is success (2xx) */
261
boolean isSuccess();
262
263
/** Check if status is redirection (3xx) */
264
boolean isRedirection();
265
266
/** Check if status is client error (4xx) */
267
boolean isClientError();
268
269
/** Check if status is server error (5xx) */
270
boolean isServerError();
271
272
/** Check if enum matches given status code */
273
boolean equals(int code);
274
}
275
}
276
```
277
278
**Usage Examples:**
279
280
```java
281
import org.eclipse.jetty.http.*;
282
283
// Working with HTTP methods
284
HttpMethod method = HttpMethod.fromString("POST");
285
if (method != null) {
286
boolean safe = method.isSafe(); // false for POST
287
boolean idempotent = method.isIdempotent(); // false for POST
288
ByteBuffer buffer = method.asBuffer(); // Efficient network serialization
289
}
290
291
// Method safety checks
292
if (HttpMethod.GET.isSafe()) {
293
// Safe to cache, no side effects
294
}
295
296
if (HttpMethod.PUT.isIdempotent()) {
297
// Safe to retry
298
}
299
300
// Efficient method comparison
301
if (method.is("GET")) {
302
// Handle GET request
303
}
304
305
// Working with status codes
306
int status = HttpStatus.OK_200;
307
String message = HttpStatus.getMessage(status); // "OK"
308
309
// Status category checks
310
boolean isError = HttpStatus.isClientError(404); // true
311
boolean isSuccess = HttpStatus.isSuccess(201); // true
312
boolean hasBody = !HttpStatus.hasNoBody(204); // false (204 No Content)
313
314
// Using status code enum
315
HttpStatus.Code code = HttpStatus.getCode(404);
316
if (code == HttpStatus.Code.NOT_FOUND) {
317
// Handle not found
318
}
319
320
// Status range checks
321
if (HttpStatus.isInformational(103)) { // true
322
// Handle interim response
323
}
324
325
if (HttpStatus.isServerError(503)) { // true
326
// Handle server error - maybe retry
327
}
328
329
// Method caching for performance
330
HttpMethod cached = HttpMethod.CACHE.get("GET"); // Case-sensitive
331
HttpMethod insensitive = HttpMethod.INSENSITIVE_CACHE.get("get"); // Case-insensitive
332
333
// Integer representations for switch statements
334
switch (method.ordinal()) {
335
case HttpMethod.GET_AS_INT:
336
// Handle GET
337
break;
338
case HttpMethod.POST_AS_INT:
339
// Handle POST
340
break;
341
case HttpMethod.PUT_AS_INT:
342
// Handle PUT
343
break;
344
case HttpMethod.HEAD_AS_INT:
345
// Handle HEAD
346
break;
347
}
348
```