tii 0.0.6

A Low-Latency Web Server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Provides functionality for handling HTTP status codes.

use crate::util::three_digit_to_utf;

/// Represents an HTTP status code.
///
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StatusCode {
  /// `100 Continue`: Continue with request.
  Continue,
  /// `101 Switching Protocols`: Protocol upgrade.
  SwitchingProtocols,
  /// `200 OK`: Request succeeded.
  OK,
  /// `201 Created`: Resource created.
  Created,
  /// `202 Accepted`: Request received, but not yet acted upon.
  Accepted,
  /// `203 Non-Authoritative Information`: Request processed, but response is from another source.
  NonAuthoritative,
  /// `204 No Content`: There is no content to send for this request.
  NoContent,
  /// `205 Reset Content`: Indicates that the document which sent this request should be reset.
  ResetContent,
  /// `206 Partial Content`: This response only contains part of a resource.
  PartialContent,
  /// `300 Multiple Choice`: The request has multiple possible responses.
  MultipleChoices,
  /// `301 Moved Permanently`: The resource has moved permanently to a new location.
  MovedPermanently,
  /// `302 Found`: The resource has moved temporarily to a new location.
  Found,
  /// `303 See Other`: The resource can be found under a different URI.
  SeeOther,
  /// `304 Not Modified`: The resource has not been modified since the last request.
  NotModified,
  /// `305 Use Proxy`: The requested resource must be accessed through a proxy.
  UseProxy,
  /// `307 Temporary Redirect`: The resource has moved temporarily to a new location.
  TemporaryRedirect,
  /// `308 Permanent Redirect`: The resource has moved permanently to a new location.
  PermanentRedirect,
  /// `400 Bad Request`: The request could not be understood by the server.
  BadRequest,
  /// `401 Unauthorized`: The request requires user authentication.
  Unauthorized,
  /// `402 Payment Required`: Reserved non-standard status code.
  /// Usually used to indicate that an application wants more money to perform an operation.
  PaymentRequired,
  /// `403 Forbidden`: The client is not allowed to access this content.
  Forbidden,
  /// `404 Not Found`: The server can not find the requested resource.
  NotFound,
  /// `405 Method Not Allowed`: The method specified in the request is not allowed for the resource.
  MethodNotAllowed,
  /// `406 Not Acceptable`: No content that meets the criteria is available.
  NotAcceptable,
  /// `407 Proxy Authentication Required`: The client must first authenticate itself with a proxy.
  ProxyAuthenticationRequired,
  /// `408 Request Timeout`: The server timed out waiting for the request.
  RequestTimeout,
  /// `409 Conflict`: The request could not be completed because of a conflict with the server's current state.
  Conflict,
  /// `410 Gone`: The requested resource is no longer available.
  Gone,
  /// `411 Length Required`: The request did not specify the length of its content.
  LengthRequired,
  /// `412 Precondition Failed`: The server does not meet one of the client's preconditions.
  PreconditionFailed,
  /// `413 Payload Too Large`: The request is larger than the server is willing or able to process.
  #[deprecated]
  RequestEntityTooLarge,
  /// `413 Content Too Large`: The request is larger than the server is willing or able to process. Newer version of RequestEntityTooLarge
  ContentTooLarge,
  /// `414 URI Too Long`: The URI provided was too long for the server to process.
  RequestURITooLong,
  /// `415 Unsupported Media Type`: The request entity has a media type which the server or resource does not support.
  UnsupportedMediaType,
  /// `416 Requested Range Not Satisfiable`: The range specified in the `Range` header cannot be fulfilled.
  RequestedRangeNotSatisfiable,
  /// `417 Expectation Failed`: The expectation given in the `Expect` header could not be met by the server.
  ExpectationFailed,
  /// `500 Internal Server Error`: The server encountered an unexpected error which prevented it from fulfilling the request.
  InternalServerError,
  /// `501 Not Implemented`: The server does not support the functionality required to fulfill the request.
  NotImplemented,
  /// `502 Bad Gateway`: The server, while acting as a gateway or proxy, received an invalid response from the upstream server.
  BadGateway,
  /// `503 Service Unavailable`: The server is temporarily unable to handle the request.
  ServiceUnavailable,
  /// `504 Gateway Timeout`: The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.
  GatewayTimeout,
  /// `505 HTTP Version Not Supported`: The server does not support the HTTP protocol version used in the request.
  VersionNotSupported,

  /// User defined status code, some applications need non-standard custom status codes.
  CustomStr(u16, [u8; 3], &'static str),
  /// User defined status code, some applications need non-standard custom status codes.
  CustomString(u16, [u8; 3], String),
}

impl StatusCode {
  /// Creates a custom Status code from a static message and code.
  /// Codes with more or less than 3 digits or status lines with invalid content will silently turn into
  /// Internal server error. This method is intended to be called from const code so you
  /// can put your custom codes into const variables.
  ///
  pub const fn from_custom(code: u16, status_line: &'static str) -> Self {
    //TODO verify status_line doesnt contain funny characters here?
    if !status_line.is_ascii() || status_line.is_empty() {
      return Self::InternalServerError;
    }

    if code < 100 || code > 999 {
      return Self::InternalServerError;
    }

    Self::CustomStr(code, three_digit_to_utf(code), status_line)
  }

  /// Creates a custom Status code from a dynamic (possibly heap allocated) message and code.
  /// # Returns
  /// None: Codes with more or less than 3 digits or status lines with invalid content
  ///
  pub fn from_custom_string<T: ToString>(code: u16, status_line: &T) -> Option<Self> {
    let status_line = status_line.to_string();

    //TODO verify status_line doesnt contain funny characters here?
    if !status_line.is_ascii() || status_line.is_empty() {
      return None;
    }

    if !(100..=999).contains(&code) {
      return None;
    }

    if let Some(well_known) = Self::from_well_known_code(code) {
      if well_known.status_line() == status_line.as_str() {
        return Some(well_known);
      }
    }

    Some(Self::CustomString(code, three_digit_to_utf(code), status_line))
  }

  /// This fn returns a status code representing a well known code that is specified in the RFC for http.
  /// If the code is not well known then the fn returns the same value it would return for "500 Internal Server Error"
  ///
  pub const fn from_well_known_code_or_500(code: u16) -> Self {
    match code {
      100 => StatusCode::Continue,
      101 => StatusCode::SwitchingProtocols,
      200 => StatusCode::OK,
      201 => StatusCode::Created,
      202 => StatusCode::Accepted,
      203 => StatusCode::NonAuthoritative,
      204 => StatusCode::NoContent,
      205 => StatusCode::ResetContent,
      206 => StatusCode::PartialContent,
      300 => StatusCode::MultipleChoices,
      301 => StatusCode::MovedPermanently,
      302 => StatusCode::Found,
      303 => StatusCode::SeeOther,
      304 => StatusCode::NotModified,
      305 => StatusCode::UseProxy,
      307 => StatusCode::TemporaryRedirect,
      400 => StatusCode::BadRequest,
      401 => StatusCode::Unauthorized,
      403 => StatusCode::Forbidden,
      404 => StatusCode::NotFound,
      405 => StatusCode::MethodNotAllowed,
      406 => StatusCode::NotAcceptable,
      407 => StatusCode::ProxyAuthenticationRequired,
      408 => StatusCode::RequestTimeout,
      409 => StatusCode::Conflict,
      410 => StatusCode::Gone,
      411 => StatusCode::LengthRequired,
      412 => StatusCode::PreconditionFailed,
      413 => StatusCode::ContentTooLarge,
      414 => StatusCode::RequestURITooLong,
      415 => StatusCode::UnsupportedMediaType,
      416 => StatusCode::RequestedRangeNotSatisfiable,
      417 => StatusCode::ExpectationFailed,
      501 => StatusCode::NotImplemented,
      502 => StatusCode::BadGateway,
      503 => StatusCode::ServiceUnavailable,
      504 => StatusCode::GatewayTimeout,
      505 => StatusCode::VersionNotSupported,
      _ => StatusCode::InternalServerError,
    }
  }

  /// This fn returns a status code representing a well known code that is specified in the RFC for http.
  /// # Returns
  /// None: the code was not well known.
  ///
  pub const fn from_well_known_code(code: u16) -> Option<Self> {
    Some(match code {
      100 => StatusCode::Continue,
      101 => StatusCode::SwitchingProtocols,
      200 => StatusCode::OK,
      201 => StatusCode::Created,
      202 => StatusCode::Accepted,
      203 => StatusCode::NonAuthoritative,
      204 => StatusCode::NoContent,
      205 => StatusCode::ResetContent,
      206 => StatusCode::PartialContent,
      300 => StatusCode::MultipleChoices,
      301 => StatusCode::MovedPermanently,
      302 => StatusCode::Found,
      303 => StatusCode::SeeOther,
      304 => StatusCode::NotModified,
      305 => StatusCode::UseProxy,
      307 => StatusCode::TemporaryRedirect,
      400 => StatusCode::BadRequest,
      401 => StatusCode::Unauthorized,
      403 => StatusCode::Forbidden,
      404 => StatusCode::NotFound,
      405 => StatusCode::MethodNotAllowed,
      406 => StatusCode::NotAcceptable,
      407 => StatusCode::ProxyAuthenticationRequired,
      408 => StatusCode::RequestTimeout,
      409 => StatusCode::Conflict,
      410 => StatusCode::Gone,
      411 => StatusCode::LengthRequired,
      412 => StatusCode::PreconditionFailed,
      413 => StatusCode::ContentTooLarge,
      414 => StatusCode::RequestURITooLong,
      415 => StatusCode::UnsupportedMediaType,
      416 => StatusCode::RequestedRangeNotSatisfiable,
      417 => StatusCode::ExpectationFailed,
      500 => StatusCode::InternalServerError,
      501 => StatusCode::NotImplemented,
      502 => StatusCode::BadGateway,
      503 => StatusCode::ServiceUnavailable,
      504 => StatusCode::GatewayTimeout,
      505 => StatusCode::VersionNotSupported,
      _ => return None,
    })
  }

  /// Returns the status line as an Option<&'static str>
  /// This fn will return None for heap allocated status lines.
  pub const fn status_line_static(&self) -> Option<&'static str> {
    Some(match self {
      StatusCode::Continue => "Continue",
      StatusCode::SwitchingProtocols => "Switching Protocols",
      StatusCode::OK => "OK",
      StatusCode::Created => "Created",
      StatusCode::Accepted => "Accepted",
      StatusCode::NonAuthoritative => "Non-Authoritative Information",
      StatusCode::NoContent => "No Content",
      StatusCode::ResetContent => "Reset Content",
      StatusCode::PartialContent => "Partial Content",
      StatusCode::MultipleChoices => "Multiple Choices",
      StatusCode::MovedPermanently => "Moved Permanently",
      StatusCode::Found => "Found",
      StatusCode::SeeOther => "See Other",
      StatusCode::NotModified => "Not Modified",
      StatusCode::UseProxy => "Use Proxy",
      StatusCode::TemporaryRedirect => "Temporary Redirect",
      StatusCode::BadRequest => "Bad Request",
      StatusCode::Unauthorized => "Unauthorized",
      StatusCode::Forbidden => "Forbidden",
      StatusCode::NotFound => "Not Found",
      StatusCode::MethodNotAllowed => "Method Not Allowed",
      StatusCode::NotAcceptable => "Not Acceptable",
      StatusCode::ProxyAuthenticationRequired => "Proxy Authentication Required",
      StatusCode::RequestTimeout => "Request Timeout",
      StatusCode::Conflict => "Conflict",
      StatusCode::Gone => "Gone",
      StatusCode::LengthRequired => "Length Required",
      StatusCode::PreconditionFailed => "Precondition Failed",
      #[expect(deprecated)]
      StatusCode::RequestEntityTooLarge => "Request Entity Too Large",
      StatusCode::ContentTooLarge => "Content Too Large",
      StatusCode::RequestURITooLong => "Request-URI Too Long",
      StatusCode::UnsupportedMediaType => "Unsupported Media Type",
      StatusCode::RequestedRangeNotSatisfiable => "Requested Range Not Satisfiable",
      StatusCode::ExpectationFailed => "Expectation Failed",
      StatusCode::InternalServerError => "Internal Server Error",
      StatusCode::NotImplemented => "Not Implemented",
      StatusCode::BadGateway => "Bad Gateway",
      StatusCode::ServiceUnavailable => "Service Unavailable",
      StatusCode::GatewayTimeout => "Gateway Timeout",
      StatusCode::VersionNotSupported => "HTTP Version Not Supported",
      StatusCode::PermanentRedirect => "Permanent Redirect",
      StatusCode::PaymentRequired => "Payment Required",
      StatusCode::CustomStr(_, _, str) => str,
      StatusCode::CustomString(_, _, _) => return None,
    })
  }

  /// Returns the status line as a &str
  pub fn status_line(&self) -> &str {
    match self {
      StatusCode::Continue => "Continue",
      StatusCode::SwitchingProtocols => "Switching Protocols",
      StatusCode::OK => "OK",
      StatusCode::Created => "Created",
      StatusCode::Accepted => "Accepted",
      StatusCode::NonAuthoritative => "Non-Authoritative Information",
      StatusCode::NoContent => "No Content",
      StatusCode::ResetContent => "Reset Content",
      StatusCode::PartialContent => "Partial Content",
      StatusCode::MultipleChoices => "Multiple Choices",
      StatusCode::MovedPermanently => "Moved Permanently",
      StatusCode::Found => "Found",
      StatusCode::SeeOther => "See Other",
      StatusCode::NotModified => "Not Modified",
      StatusCode::UseProxy => "Use Proxy",
      StatusCode::TemporaryRedirect => "Temporary Redirect",
      StatusCode::BadRequest => "Bad Request",
      StatusCode::Unauthorized => "Unauthorized",
      StatusCode::Forbidden => "Forbidden",
      StatusCode::NotFound => "Not Found",
      StatusCode::MethodNotAllowed => "Method Not Allowed",
      StatusCode::NotAcceptable => "Not Acceptable",
      StatusCode::ProxyAuthenticationRequired => "Proxy Authentication Required",
      StatusCode::RequestTimeout => "Request Timeout",
      StatusCode::Conflict => "Conflict",
      StatusCode::Gone => "Gone",
      StatusCode::LengthRequired => "Length Required",
      StatusCode::PreconditionFailed => "Precondition Failed",
      #[expect(deprecated)]
      StatusCode::RequestEntityTooLarge => "Request Entity Too Large",
      StatusCode::ContentTooLarge => "Content Too Large",
      StatusCode::RequestURITooLong => "Request-URI Too Long",
      StatusCode::UnsupportedMediaType => "Unsupported Media Type",
      StatusCode::RequestedRangeNotSatisfiable => "Requested Range Not Satisfiable",
      StatusCode::ExpectationFailed => "Expectation Failed",
      StatusCode::InternalServerError => "Internal Server Error",
      StatusCode::NotImplemented => "Not Implemented",
      StatusCode::BadGateway => "Bad Gateway",
      StatusCode::ServiceUnavailable => "Service Unavailable",
      StatusCode::GatewayTimeout => "Gateway Timeout",
      StatusCode::VersionNotSupported => "HTTP Version Not Supported",
      StatusCode::PermanentRedirect => "Permanent Redirect",
      StatusCode::PaymentRequired => "Payment Required",
      StatusCode::CustomStr(_, _, str) => str,
      StatusCode::CustomString(_, _, str) => str.as_str(),
    }
  }

  /// Returns the 3-digit code as 3 byte utf-8 representation.
  pub const fn code_as_utf(&self) -> &[u8; 3] {
    match self {
      StatusCode::Continue => b"100",
      StatusCode::SwitchingProtocols => b"101",
      StatusCode::OK => b"200",
      StatusCode::Created => b"201",
      StatusCode::Accepted => b"202",
      StatusCode::NonAuthoritative => b"203",
      StatusCode::NoContent => b"204",
      StatusCode::ResetContent => b"205",
      StatusCode::PartialContent => b"206",
      StatusCode::MultipleChoices => b"300",
      StatusCode::MovedPermanently => b"301",
      StatusCode::Found => b"302",
      StatusCode::SeeOther => b"303",
      StatusCode::NotModified => b"304",
      StatusCode::UseProxy => b"305",
      StatusCode::TemporaryRedirect => b"307",
      StatusCode::PermanentRedirect => b"308",
      StatusCode::BadRequest => b"400",
      StatusCode::PaymentRequired => b"402",
      StatusCode::Unauthorized => b"401",
      StatusCode::Forbidden => b"403",
      StatusCode::NotFound => b"404",
      StatusCode::MethodNotAllowed => b"405",
      StatusCode::NotAcceptable => b"406",
      StatusCode::ProxyAuthenticationRequired => b"407",
      StatusCode::RequestTimeout => b"408",
      StatusCode::Conflict => b"409",
      StatusCode::Gone => b"410",
      StatusCode::LengthRequired => b"411",
      StatusCode::PreconditionFailed => b"412",
      #[expect(deprecated)]
      StatusCode::RequestEntityTooLarge => b"413",
      StatusCode::ContentTooLarge => b"413",
      StatusCode::RequestURITooLong => b"414",
      StatusCode::UnsupportedMediaType => b"415",
      StatusCode::RequestedRangeNotSatisfiable => b"416",
      StatusCode::ExpectationFailed => b"417",
      StatusCode::InternalServerError => b"500",
      StatusCode::NotImplemented => b"501",
      StatusCode::BadGateway => b"502",
      StatusCode::ServiceUnavailable => b"503",
      StatusCode::GatewayTimeout => b"504",
      StatusCode::VersionNotSupported => b"505",
      StatusCode::CustomStr(_, code, _) => code,
      StatusCode::CustomString(_, code, _) => code,
    }
  }

  /// Returns the code as u16. This value is guaranteed to be in >= 100 <= 999 range.
  pub const fn code(&self) -> u16 {
    match self {
      StatusCode::Continue => 100,
      StatusCode::SwitchingProtocols => 101,
      StatusCode::OK => 200,
      StatusCode::Created => 201,
      StatusCode::Accepted => 202,
      StatusCode::NonAuthoritative => 203,
      StatusCode::NoContent => 204,
      StatusCode::ResetContent => 205,
      StatusCode::PartialContent => 206,
      StatusCode::MultipleChoices => 300,
      StatusCode::MovedPermanently => 301,
      StatusCode::Found => 302,
      StatusCode::SeeOther => 303,
      StatusCode::NotModified => 304,
      StatusCode::UseProxy => 305,
      StatusCode::TemporaryRedirect => 307,
      StatusCode::PermanentRedirect => 308,
      StatusCode::BadRequest => 400,
      StatusCode::Unauthorized => 401,
      StatusCode::PaymentRequired => 402,
      StatusCode::Forbidden => 403,
      StatusCode::NotFound => 404,
      StatusCode::MethodNotAllowed => 405,
      StatusCode::NotAcceptable => 406,
      StatusCode::ProxyAuthenticationRequired => 407,
      StatusCode::RequestTimeout => 408,
      StatusCode::Conflict => 409,
      StatusCode::Gone => 410,
      StatusCode::LengthRequired => 411,
      StatusCode::PreconditionFailed => 412,
      #[expect(deprecated)]
      StatusCode::RequestEntityTooLarge => 413,
      StatusCode::ContentTooLarge => 413,
      StatusCode::RequestURITooLong => 414,
      StatusCode::UnsupportedMediaType => 415,
      StatusCode::RequestedRangeNotSatisfiable => 416,
      StatusCode::ExpectationFailed => 417,
      StatusCode::InternalServerError => 500,
      StatusCode::NotImplemented => 501,
      StatusCode::BadGateway => 502,
      StatusCode::ServiceUnavailable => 503,
      StatusCode::GatewayTimeout => 504,
      StatusCode::VersionNotSupported => 505,
      StatusCode::CustomStr(code, _, _) => *code,
      StatusCode::CustomString(code, _, _) => *code,
    }
  }
}