Skip to content

Commit fe9bf99

Browse files
authored
Introduce StatusCodes module (#982)
1 parent a2bc2e0 commit fe9bf99

File tree

5 files changed

+176
-82
lines changed

5 files changed

+176
-82
lines changed

src/HTTP.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ include("multipart.jl") ;using .Forms
3838
include("Parsers.jl") ;import .Parsers: Headers, Header,
3939
ParseError
4040
include("ConnectionPool.jl") ;using .ConnectionPool
41+
include("StatusCodes.jl") ;using .StatusCodes
4142
include("Messages.jl") ;using .Messages
4243
include("cookies.jl") ;using .Cookies
4344
include("Streams.jl") ;using .Streams

src/Messages.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export Message, Request, Response,
6161

6262
using URIs, CodecZlib
6363
using ..Pairs, ..IOExtras, ..Parsers, ..Strings, ..Forms, ..Conditions
64-
using ..ConnectionPool
64+
using ..ConnectionPool, ..StatusCodes
6565

6666
const nobody = UInt8[]
6767
const unknown_length = typemax(Int)
@@ -480,7 +480,7 @@ function writestartline(io::IO, r::Request)
480480
end
481481

482482
function writestartline(io::IO, r::Response)
483-
return write(io, r.version, " ", string(r.status), " ", statustext(r.status), "\r\n")
483+
return write(io, r.version, " ", string(r.status), " ", StatusCodes.statustext(r.status), "\r\n")
484484
end
485485

486486
"""
@@ -619,6 +619,9 @@ function Base.show(io::IO, m::Message)
619619
return
620620
end
621621

622-
include("status_messages.jl")
622+
function statustext(status)
623+
Base.depwarn("`Messages.statustext` is deprecated, use `StatusCodes.statustext` instead.", :statustext)
624+
return StatusCodes.statustext(status)
625+
end
623626

624627
end # module Messages

src/StatusCodes.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
This module provides HTTP status code constatnts and related functions
3+
"""
4+
module StatusCodes
5+
6+
export statustext
7+
8+
# Status code definitions
9+
const CONTINUE = 100
10+
const SWITCHING_PROTOCOLS = 101
11+
const PROCESSING = 102
12+
const EARLY_HINTS = 103
13+
const OK = 200
14+
const CREATED = 201
15+
const ACCEPTED = 202
16+
const NON_AUTHORITATIVE_INFORMATION = 203
17+
const NO_CONTENT = 204
18+
const RESET_CONTENT = 205
19+
const PARTIAL_CONTENT = 206
20+
const MULTI_STATUS = 207
21+
const ALREADY_REPORTED = 208
22+
const IM_USED = 226
23+
const MULTIPLE_CHOICES = 300
24+
const MOVED_PERMANENTLY = 301
25+
const MOVED_TEMPORARILY = 302
26+
const SEE_OTHER = 303
27+
const NOT_MODIFIED = 304
28+
const USE_PROXY = 305
29+
const TEMPORARY_REDIRECT = 307
30+
const PERMANENT_REDIRECT = 308
31+
const BAD_REQUEST = 400
32+
const UNAUTHORIZED = 401
33+
const PAYMENT_REQUIRED = 402
34+
const FORBIDDEN = 403
35+
const NOT_FOUND = 404
36+
const METHOD_NOT_ALLOWED = 405
37+
const NOT_ACCEPTABLE = 406
38+
const PROXY_AUTHENTICATION_REQUIRED = 407
39+
const REQUEST_TIME_OUT = 408
40+
const CONFLICT = 409
41+
const GONE = 410
42+
const LENGTH_REQUIRED = 411
43+
const PRECONDITION_FAILED = 412
44+
const REQUEST_ENTITY_TOO_LARGE = 413
45+
const REQUEST_URI_TOO_LARGE = 414
46+
const UNSUPPORTED_MEDIA_TYPE = 415
47+
const REQUESTED_RANGE_NOT_SATISFIABLE = 416
48+
const EXPECTATION_FAILED = 417
49+
const IM_A_TEAPOT = 418
50+
const MISDIRECTED_REQUEST = 421
51+
const UNPROCESSABLE_ENTITY = 422
52+
const LOCKED = 423
53+
const FAILED_DEPENDENCY = 424
54+
const UNORDERED_COLLECTION = 425
55+
const UPGRADE_REQUIRED = 426
56+
const PRECONDITION_REQUIRED = 428
57+
const TOO_MANY_REQUESTS = 429
58+
const REQUEST_HEADER_FIELDS_TOO_LARGE = 431
59+
const LOGIN_TIMEOUT = 440
60+
const NGINX_ERROR_NO_RESPONSE = 444
61+
const UNAVAILABLE_FOR_LEGAL_REASONS = 451
62+
const NGINX_ERROR_SSL_CERTIFICATE_ERROR = 495
63+
const NGINX_ERROR_SSL_CERTIFICATE_REQUIRED = 496
64+
const NGINX_ERROR_HTTP_TO_HTTPS = 497
65+
const NGINX_ERROR_OR_ANTIVIRUS_INTERCEPTED_REQUEST_OR_ARCGIS_ERROR = 499
66+
const INTERNAL_SERVER_ERROR = 500
67+
const NOT_IMPLEMENTED = 501
68+
const BAD_GATEWAY = 502
69+
const SERVICE_UNAVAILABLE = 503
70+
const GATEWAY_TIME_OUT = 504
71+
const HTTP_VERSION_NOT_SUPPORTED = 505
72+
const VARIANT_ALSO_NEGOTIATES = 506
73+
const INSUFFICIENT_STORAGE = 507
74+
const LOOP_DETECTED = 508
75+
const BANDWIDTH_LIMIT_EXCEEDED = 509
76+
const NOT_EXTENDED = 510
77+
const NETWORK_AUTHENTICATION_REQUIRED = 511
78+
const CLOUDFLARE_SERVER_ERROR_UNKNOWN = 520
79+
const CLOUDFLARE_SERVER_ERROR_CONNECTION_REFUSED = 521
80+
const CLOUDFLARE_SERVER_ERROR_CONNECTION_TIMEOUT = 522
81+
const CLOUDFLARE_SERVER_ERROR_ORIGIN_SERVER_UNREACHABLE = 523
82+
const CLOUDFLARE_SERVER_ERROR_A_TIMEOUT = 524
83+
const CLOUDFLARE_SERVER_ERROR_CONNECTION_FAILED = 525
84+
const CLOUDFLARE_SERVER_ERROR_INVALID_SSL_CERITIFICATE = 526
85+
const CLOUDFLARE_SERVER_ERROR_RAILGUN_ERROR = 527
86+
const SITE_FROZEN = 530
87+
88+
include("status_messages.jl")
89+
90+
end # module StatusCodes

src/status_messages.jl

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,83 @@ statustext(status) = Base.get(STATUS_MESSAGES, status, "Unknown Code")
1616

1717
const STATUS_MESSAGES = (()->begin
1818
v = fill("Unknown Code", 530)
19-
v[100] = "Continue"
20-
v[101] = "Switching Protocols"
21-
v[102] = "Processing" # RFC 2518 => obsoleted by RFC 4918
22-
v[103] = "Early Hints"
23-
v[200] = "OK"
24-
v[201] = "Created"
25-
v[202] = "Accepted"
26-
v[203] = "Non-Authoritative Information"
27-
v[204] = "No Content"
28-
v[205] = "Reset Content"
29-
v[206] = "Partial Content"
30-
v[207] = "Multi-Status" # RFC 4918
31-
v[208] = "Already Reported" # RFC5842
32-
v[226] = "IM Used" # RFC3229
33-
v[300] = "Multiple Choices"
34-
v[301] = "Moved Permanently"
35-
v[302] = "Moved Temporarily"
36-
v[303] = "See Other"
37-
v[304] = "Not Modified"
38-
v[305] = "Use Proxy"
39-
v[307] = "Temporary Redirect"
40-
v[308] = "Permanent Redirect" # RFC7238
41-
v[400] = "Bad Request"
42-
v[401] = "Unauthorized"
43-
v[402] = "Payment Required"
44-
v[403] = "Forbidden"
45-
v[404] = "Not Found"
46-
v[405] = "Method Not Allowed"
47-
v[406] = "Not Acceptable"
48-
v[407] = "Proxy Authentication Required"
49-
v[408] = "Request Time-out"
50-
v[409] = "Conflict"
51-
v[410] = "Gone"
52-
v[411] = "Length Required"
53-
v[412] = "Precondition Failed"
54-
v[413] = "Request Entity Too Large"
55-
v[414] = "Request-URI Too Large"
56-
v[415] = "Unsupported Media Type"
57-
v[416] = "Requested Range Not Satisfiable"
58-
v[417] = "Expectation Failed"
59-
v[418] = "I'm a teapot" # RFC 2324
60-
v[421] = "Misdirected Request" # RFC 7540
61-
v[422] = "Unprocessable Entity" # RFC 4918
62-
v[423] = "Locked" # RFC 4918
63-
v[424] = "Failed Dependency" # RFC 4918
64-
v[425] = "Unordered Collection" # RFC 4918
65-
v[426] = "Upgrade Required" # RFC 2817
66-
v[428] = "Precondition Required" # RFC 6585
67-
v[429] = "Too Many Requests" # RFC 6585
68-
v[431] = "Request Header Fields Too Large" # RFC 6585
69-
v[440] = "Login Timeout"
70-
v[444] = "nginx error: No Response"
71-
v[451] = "Unavailable For Legal Reasons" # RFC7725
72-
v[495] = "nginx error: SSL Certificate Error"
73-
v[496] = "nginx error: SSL Certificate Required"
74-
v[497] = "nginx error: HTTP -> HTTPS"
75-
v[499] = "nginx error or Antivirus intercepted request or ArcGIS error"
76-
v[500] = "Internal Server Error"
77-
v[501] = "Not Implemented"
78-
v[502] = "Bad Gateway"
79-
v[503] = "Service Unavailable"
80-
v[504] = "Gateway Time-out"
81-
v[505] = "HTTP Version Not Supported"
82-
v[506] = "Variant Also Negotiates" # RFC 2295
83-
v[507] = "Insufficient Storage" # RFC 4918
84-
v[508] = "Loop Detected" # RFC5842
85-
v[509] = "Bandwidth Limit Exceeded"
86-
v[510] = "Not Extended" # RFC 2774
87-
v[511] = "Network Authentication Required" # RFC 6585
88-
v[520] = "CloudFlare Server Error: Unknown"
89-
v[521] = "CloudFlare Server Error: Connection Refused"
90-
v[522] = "CloudFlare Server Error: Connection Timeout"
91-
v[523] = "CloudFlare Server Error: Origin Server Unreachable"
92-
v[524] = "CloudFlare Server Error: Connection Timeout"
93-
v[525] = "CloudFlare Server Error: Connection Failed"
94-
v[526] = "CloudFlare Server Error: Invalid SSL Ceritificate"
95-
v[527] = "CloudFlare Server Error: Railgun Error"
96-
v[530] = "Site Frozen"
19+
v[CONTINUE] = "Continue"
20+
v[SWITCHING_PROTOCOLS] = "Switching Protocols"
21+
v[PROCESSING] = "Processing" # RFC 2518 => obsoleted by RFC 4918
22+
v[EARLY_HINTS] = "Early Hints"
23+
v[OK] = "OK"
24+
v[CREATED] = "Created"
25+
v[ACCEPTED] = "Accepted"
26+
v[NON_AUTHORITATIVE_INFORMATION] = "Non-Authoritative Information"
27+
v[NO_CONTENT] = "No Content"
28+
v[RESET_CONTENT] = "Reset Content"
29+
v[PARTIAL_CONTENT] = "Partial Content"
30+
v[MULTI_STATUS] = "Multi-Status" # RFC4918
31+
v[ALREADY_REPORTED] = "Already Reported" # RFC5842
32+
v[IM_USED] = "IM Used" # RFC3229
33+
v[MULTIPLE_CHOICES] = "Multiple Choices"
34+
v[MOVED_PERMANENTLY] = "Moved Permanently"
35+
v[MOVED_TEMPORARILY] = "Moved Temporarily"
36+
v[SEE_OTHER] = "See Other"
37+
v[NOT_MODIFIED] = "Not Modified"
38+
v[USE_PROXY] = "Use Proxy"
39+
v[TEMPORARY_REDIRECT] = "Temporary Redirect"
40+
v[PERMANENT_REDIRECT] = "Permanent Redirect" # RFC7238
41+
v[BAD_REQUEST] = "Bad Request"
42+
v[UNAUTHORIZED] = "Unauthorized"
43+
v[PAYMENT_REQUIRED] = "Payment Required"
44+
v[FORBIDDEN] = "Forbidden"
45+
v[NOT_FOUND] = "Not Found"
46+
v[METHOD_NOT_ALLOWED] = "Method Not Allowed"
47+
v[NOT_ACCEPTABLE] = "Not Acceptable"
48+
v[PROXY_AUTHENTICATION_REQUIRED] = "Proxy Authentication Required"
49+
v[REQUEST_TIME_OUT] = "Request Time-out"
50+
v[CONFLICT] = "Conflict"
51+
v[GONE] = "Gone"
52+
v[LENGTH_REQUIRED] = "Length Required"
53+
v[PRECONDITION_FAILED] = "Precondition Failed"
54+
v[REQUEST_ENTITY_TOO_LARGE] = "Request Entity Too Large"
55+
v[REQUEST_URI_TOO_LARGE] = "Request-URI Too Large"
56+
v[UNSUPPORTED_MEDIA_TYPE] = "Unsupported Media Type"
57+
v[REQUESTED_RANGE_NOT_SATISFIABLE] = "Requested Range Not Satisfiable"
58+
v[EXPECTATION_FAILED] = "Expectation Failed"
59+
v[IM_A_TEAPOT] = "I'm a teapot" # RFC 2324
60+
v[MISDIRECTED_REQUEST] = "Misdirected Request" # RFC 7540
61+
v[UNPROCESSABLE_ENTITY] = "Unprocessable Entity" # RFC 4918
62+
v[LOCKED] = "Locked" # RFC 4918
63+
v[FAILED_DEPENDENCY] = "Failed Dependency" # RFC 4918
64+
v[UNORDERED_COLLECTION] = "Unordered Collection" # RFC 4918
65+
v[UPGRADE_REQUIRED] = "Upgrade Required" # RFC 2817
66+
v[PRECONDITION_REQUIRED] = "Precondition Required" # RFC 6585
67+
v[TOO_MANY_REQUESTS] = "Too Many Requests" # RFC 6585
68+
v[REQUEST_HEADER_FIELDS_TOO_LARGE] = "Request Header Fields Too Large" # RFC 6585
69+
v[LOGIN_TIMEOUT] = "Login Timeout"
70+
v[NGINX_ERROR_NO_RESPONSE] = "nginx error: No Response"
71+
v[UNAVAILABLE_FOR_LEGAL_REASONS] = "Unavailable For Legal Reasons" # RFC7725
72+
v[NGINX_ERROR_SSL_CERTIFICATE_ERROR] = "nginx error: SSL Certificate Error"
73+
v[NGINX_ERROR_SSL_CERTIFICATE_REQUIRED] = "nginx error: SSL Certificate Required"
74+
v[NGINX_ERROR_HTTP_TO_HTTPS] = "nginx error: HTTP -> HTTPS"
75+
v[NGINX_ERROR_OR_ANTIVIRUS_INTERCEPTED_REQUEST_OR_ARCGIS_ERROR] = "nginx error or Antivirus intercepted request or ArcGIS error"
76+
v[INTERNAL_SERVER_ERROR] = "Internal Server Error"
77+
v[NOT_IMPLEMENTED] = "Not Implemented"
78+
v[BAD_GATEWAY] = "Bad Gateway"
79+
v[SERVICE_UNAVAILABLE] = "Service Unavailable"
80+
v[GATEWAY_TIME_OUT] = "Gateway Time-out"
81+
v[HTTP_VERSION_NOT_SUPPORTED] = "HTTP Version Not Supported"
82+
v[VARIANT_ALSO_NEGOTIATES] = "Variant Also Negotiates" # RFC 2295
83+
v[INSUFFICIENT_STORAGE] = "Insufficient Storage" # RFC 4918
84+
v[LOOP_DETECTED] = "Loop Detected" # RFC5842
85+
v[BANDWIDTH_LIMIT_EXCEEDED] = "Bandwidth Limit Exceeded"
86+
v[NOT_EXTENDED] = "Not Extended" # RFC 2774
87+
v[NETWORK_AUTHENTICATION_REQUIRED] = "Network Authentication Required" # RFC 6585
88+
v[CLOUDFLARE_SERVER_ERROR_UNKNOWN] = "CloudFlare Server Error: Unknown"
89+
v[CLOUDFLARE_SERVER_ERROR_CONNECTION_REFUSED] = "CloudFlare Server Error: Connection Refused"
90+
v[CLOUDFLARE_SERVER_ERROR_CONNECTION_TIMEOUT] = "CloudFlare Server Error: Connection Timeout"
91+
v[CLOUDFLARE_SERVER_ERROR_ORIGIN_SERVER_UNREACHABLE] = "CloudFlare Server Error: Origin Server Unreachable"
92+
v[CLOUDFLARE_SERVER_ERROR_A_TIMEOUT] = "CloudFlare Server Error: A Timeout"
93+
v[CLOUDFLARE_SERVER_ERROR_CONNECTION_FAILED] = "CloudFlare Server Error: Connection Failed"
94+
v[CLOUDFLARE_SERVER_ERROR_INVALID_SSL_CERITIFICATE] = "CloudFlare Server Error: Invalid SSL Ceritificate"
95+
v[CLOUDFLARE_SERVER_ERROR_RAILGUN_ERROR] = "CloudFlare Server Error: Railgun Error"
96+
v[SITE_FROZEN] = "Site Frozen"
9797
return v
9898
end)()

test/parser.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ end
281281
@test r.version.major == response.http_major
282282
@test r.version.minor == response.http_minor
283283
@test r.status == response.status_code
284-
@test HTTP.Messages.statustext(r.status) == response.response_status
284+
@test HTTP.StatusCodes.statustext(r.status) == response.response_status
285285
@test length(r.headers) == response.num_headers
286286
@test r_headers == response.headers
287287
@test String(r.body) == response.body

0 commit comments

Comments
 (0)