Skip to content

Commit 2b83c3d

Browse files
singku-chinasingku
andauthored
Add Http Status Code and Header Utils
Removed private http status code definitions. Added public http status code definitions and filled the list with common status codes. Added aws_http_headers_has utils API to test if a header is in a header set. Co-authored-by: singku <tangliang551@gmail.com>
1 parent 5bda807 commit 2b83c3d

File tree

12 files changed

+208
-112
lines changed

12 files changed

+208
-112
lines changed

include/aws/http/private/http_impl.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,6 @@ enum aws_http_header_name {
4343
AWS_HTTP_HEADER_COUNT, /* Number of enums */
4444
};
4545

46-
/**
47-
* Status codes that affect internal processing.
48-
* This is NOT a definitive list of codes.
49-
*/
50-
enum aws_http_status {
51-
AWS_HTTP_STATUS_UNKNOWN = -1, /* Invalid status code. Not using 0 because it's technically a legal value */
52-
AWS_HTTP_STATUS_100_CONTINUE = 100,
53-
AWS_HTTP_STATUS_101_SWITCHING_PROTOCOLS = 101,
54-
AWS_HTTP_STATUS_200_OK = 200,
55-
AWS_HTTP_STATUS_204_NO_CONTENT = 204,
56-
AWS_HTTP_STATUS_304_NOT_MODIFIED = 304,
57-
};
58-
5946
AWS_EXTERN_C_BEGIN
6047

6148
AWS_HTTP_API void aws_http_fatal_assert_library_initialized(void);

include/aws/http/request_response.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ int aws_http_headers_get(
414414
struct aws_byte_cursor name,
415415
struct aws_byte_cursor *out_value);
416416

417+
/**
418+
* Test if header name exists or not in headers
419+
*/
420+
AWS_HTTP_API
421+
bool aws_http_headers_has(const struct aws_http_headers *headers, struct aws_byte_cursor name);
422+
417423
/**
418424
* Remove all headers with this name.
419425
* AWS_ERROR_HTTP_HEADER_NOT_FOUND is raised if no headers with this name are found.

include/aws/http/status_code.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#ifndef AWS_HTTP_STATUS_CODE_H
2+
#define AWS_HTTP_STATUS_CODE_H
3+
4+
/*
5+
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License").
8+
* You may not use this file except in compliance with the License.
9+
* A copy of the License is located at
10+
*
11+
* http://aws.amazon.com/apache2.0
12+
*
13+
* or in the "license" file accompanying this file. This file is distributed
14+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15+
* express or implied. See the License for the specific language governing
16+
* permissions and limitations under the License.
17+
*/
18+
19+
/*
20+
* Define most of the http response codes we probably will use.
21+
* https://www.iana.org/assignments/http-status-codes/http-status-codes.txt
22+
* This is NOT a definitive list of codes.
23+
*/
24+
enum aws_http_status_code {
25+
/*
26+
* This is a special response code defined for convenience in error processing,
27+
* indicating processing of http request met error and didn't reach server.
28+
*/
29+
AWS_HTTP_STATUS_CODE_UNKNOWN = -1,
30+
AWS_HTTP_STATUS_CODE_100_CONTINUE = 100,
31+
AWS_HTTP_STATUS_CODE_101_SWITCHING_PROTOCOLS = 101,
32+
AWS_HTTP_STATUS_CODE_102_PROCESSING = 102,
33+
AWS_HTTP_STATUS_CODE_103_EARLY_HINTS = 103,
34+
AWS_HTTP_STATUS_CODE_200_OK = 200,
35+
AWS_HTTP_STATUS_CODE_201_CREATED = 201,
36+
AWS_HTTP_STATUS_CODE_202_ACCEPTED = 202,
37+
AWS_HTTP_STATUS_CODE_203_NON_AUTHORITATIVE_INFORMATION = 203,
38+
AWS_HTTP_STATUS_CODE_204_NO_CONTENT = 204,
39+
AWS_HTTP_STATUS_CODE_205_RESET_CONTENT = 205,
40+
AWS_HTTP_STATUS_CODE_206_PARTIAL_CONTENT = 206,
41+
AWS_HTTP_STATUS_CODE_207_MULTI_STATUS = 207,
42+
AWS_HTTP_STATUS_CODE_208_ALREADY_REPORTED = 208,
43+
AWS_HTTP_STATUS_CODE_226_IM_USED = 226,
44+
AWS_HTTP_STATUS_CODE_300_MULTIPLE_CHOICES = 300,
45+
AWS_HTTP_STATUS_CODE_301_MOVED_PERMANENTLY = 301,
46+
AWS_HTTP_STATUS_CODE_302_FOUND = 302,
47+
AWS_HTTP_STATUS_CODE_303_SEE_OTHER = 303,
48+
AWS_HTTP_STATUS_CODE_304_NOT_MODIFIED = 304,
49+
AWS_HTTP_STATUS_CODE_305_USE_PROXY = 305,
50+
AWS_HTTP_STATUS_CODE_307_TEMPORARY_REDIRECT = 307,
51+
AWS_HTTP_STATUS_CODE_308_PERMANENT_REDIRECT = 308,
52+
AWS_HTTP_STATUS_CODE_400_BAD_REQUEST = 400,
53+
AWS_HTTP_STATUS_CODE_401_UNAUTHORIZED = 401,
54+
AWS_HTTP_STATUS_CODE_402_PAYMENT_REQUIRED = 402,
55+
AWS_HTTP_STATUS_CODE_403_FORBIDDEN = 403,
56+
AWS_HTTP_STATUS_CODE_404_NOT_FOUND = 404,
57+
AWS_HTTP_STATUS_CODE_405_METHOD_NOT_ALLOWED = 405,
58+
AWS_HTTP_STATUS_CODE_406_NOT_ACCEPTABLE = 406,
59+
AWS_HTTP_STATUS_CODE_407_PROXY_AUTHENTICATION_REQUIRED = 407,
60+
AWS_HTTP_STATUS_CODE_408_REQUEST_TIMEOUT = 408,
61+
AWS_HTTP_STATUS_CODE_409_CONFLICT = 409,
62+
AWS_HTTP_STATUS_CODE_410_GONE = 410,
63+
AWS_HTTP_STATUS_CODE_411_LENGTH_REQUIRED = 411,
64+
AWS_HTTP_STATUS_CODE_412_PRECONDITION_FAILED = 412,
65+
AWS_HTTP_STATUS_CODE_413_REQUEST_ENTITY_TOO_LARGE = 413,
66+
AWS_HTTP_STATUS_CODE_414_REQUEST_URI_TOO_LONG = 414,
67+
AWS_HTTP_STATUS_CODE_415_UNSUPPORTED_MEDIA_TYPE = 415,
68+
AWS_HTTP_STATUS_CODE_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
69+
AWS_HTTP_STATUS_CODE_417_EXPECTATION_FAILED = 417,
70+
AWS_HTTP_STATUS_CODE_421_MISDIRECTED_REQUEST = 421,
71+
AWS_HTTP_STATUS_CODE_422_UNPROCESSABLE_ENTITY = 422,
72+
AWS_HTTP_STATUS_CODE_423_LOCKED = 423,
73+
AWS_HTTP_STATUS_CODE_424_FAILED_DEPENDENCY = 424,
74+
AWS_HTTP_STATUS_CODE_425_TOO_EARLY = 425,
75+
AWS_HTTP_STATUS_CODE_426_UPGRADE_REQUIRED = 426,
76+
AWS_HTTP_STATUS_CODE_428_PRECONDITION_REQUIRED = 428,
77+
AWS_HTTP_STATUS_CODE_429_TOO_MANY_REQUESTS = 429,
78+
AWS_HTTP_STATUS_CODE_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
79+
AWS_HTTP_STATUS_CODE_451_UNAVAILABLE_FOR_LEGAL_REASON = 451,
80+
AWS_HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR = 500,
81+
AWS_HTTP_STATUS_CODE_501_NOT_IMPLEMENTED = 501,
82+
AWS_HTTP_STATUS_CODE_502_BAD_GATEWAY = 502,
83+
AWS_HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE = 503,
84+
AWS_HTTP_STATUS_CODE_504_GATEWAY_TIMEOUT = 504,
85+
AWS_HTTP_STATUS_CODE_505_HTTP_VERSION_NOT_SUPPORTED = 505,
86+
AWS_HTTP_STATUS_CODE_506_VARIANT_ALSO_NEGOTIATES = 506,
87+
AWS_HTTP_STATUS_CODE_507_INSUFFICIENT_STORAGE = 507,
88+
AWS_HTTP_STATUS_CODE_508_LOOP_DETECTED = 508,
89+
AWS_HTTP_STATUS_CODE_510_NOT_EXTENDED = 510,
90+
AWS_HTTP_STATUS_CODE_511_NETWORK_AUTHENTICATION_REQUIRED = 511,
91+
};
92+
#endif /* AWS_HTTP_STATUS_CODE_H */

source/h1_connection.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15-
#include <aws/http/private/h1_connection.h>
16-
1715
#include <aws/common/clock.h>
1816
#include <aws/common/math.h>
1917
#include <aws/common/mutex.h>
2018
#include <aws/common/string.h>
19+
#include <aws/http/private/h1_connection.h>
2120
#include <aws/http/private/h1_decoder.h>
2221
#include <aws/http/private/h1_encoder.h>
2322
#include <aws/http/private/h1_stream.h>
2423
#include <aws/http/private/request_response_impl.h>
2524
#include <aws/http/statistics.h>
25+
#include <aws/http/status_code.h>
2626
#include <aws/io/logging.h>
2727

2828
#if _MSC_VER
@@ -988,9 +988,9 @@ static int s_decoder_on_header(const struct aws_h1_decoded_header *header, void
988988
/* Certain L7 proxies send a connection close header on a 200/OK response to a CONNECT request. This is nutty
989989
* behavior, but the obviously desired behavior on a 200 CONNECT response is to leave the connection open
990990
* for the tunneling. */
991-
bool ignore_connection_close = incoming_stream->base.request_method == AWS_HTTP_METHOD_CONNECT &&
992-
incoming_stream->base.client_data &&
993-
incoming_stream->base.client_data->response_status == AWS_HTTP_STATUS_200_OK;
991+
bool ignore_connection_close =
992+
incoming_stream->base.request_method == AWS_HTTP_METHOD_CONNECT && incoming_stream->base.client_data &&
993+
incoming_stream->base.client_data->response_status == AWS_HTTP_STATUS_CODE_200_OK;
994994

995995
if (!ignore_connection_close && aws_byte_cursor_eq_c_str_ignore_case(&header->value_data, "close")) {
996996
AWS_LOGF_TRACE(
@@ -1047,7 +1047,7 @@ static int s_mark_head_done(struct aws_h1_stream *incoming_stream) {
10471047

10481048
/* Only clients can receive informational headers.
10491049
* Check whether we're switching protocols */
1050-
if (incoming_stream->base.client_data->response_status == AWS_HTTP_STATUS_101_SWITCHING_PROTOCOLS) {
1050+
if (incoming_stream->base.client_data->response_status == AWS_HTTP_STATUS_CODE_101_SWITCHING_PROTOCOLS) {
10511051

10521052
/* Switching protocols while there are multiple streams is too complex to deal with.
10531053
* Ensure stream_list has exactly this 1 stream in it. */

source/h1_decoder.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
#include <aws/http/private/h1_decoder.h>
17-
1816
#include <aws/common/string.h>
17+
#include <aws/http/private/h1_decoder.h>
1918
#include <aws/http/private/strutil.h>
19+
#include <aws/http/status_code.h>
2020
#include <aws/io/logging.h>
2121

2222
AWS_STATIC_STRING_FROM_LITERAL(s_transfer_coding_chunked, "chunked");
@@ -686,8 +686,8 @@ static int s_linestate_response(struct aws_h1_decoder *decoder, struct aws_byte_
686686
int code_val = (int)code_val_u64;
687687

688688
/* RFC-7230 section 3.3 Message Body */
689-
decoder->body_headers_ignored |= code_val == AWS_HTTP_STATUS_304_NOT_MODIFIED;
690-
decoder->body_headers_forbidden = code_val == AWS_HTTP_STATUS_204_NO_CONTENT || code_val / 100 == 1;
689+
decoder->body_headers_ignored |= code_val == AWS_HTTP_STATUS_CODE_304_NOT_MODIFIED;
690+
decoder->body_headers_forbidden = code_val == AWS_HTTP_STATUS_CODE_204_NO_CONTENT || code_val / 100 == 1;
691691

692692
if (s_check_info_response_status_code(code_val)) {
693693
decoder->header_block = AWS_HTTP_HEADER_BLOCK_INFORMATIONAL;

source/h1_encoder.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* permissions and limitations under the License.
1414
*/
1515
#include <aws/http/private/h1_encoder.h>
16-
1716
#include <aws/http/private/strutil.h>
17+
#include <aws/http/status_code.h>
1818
#include <aws/io/logging.h>
1919
#include <aws/io/stream.h>
2020

@@ -230,8 +230,8 @@ int aws_h1_encoder_message_init_from_response(
230230
* no body needed in the response
231231
* RFC-7230 section 3.3 Message Body
232232
*/
233-
body_headers_ignored |= status_int == AWS_HTTP_STATUS_304_NOT_MODIFIED;
234-
bool body_headers_forbidden = status_int == AWS_HTTP_STATUS_204_NO_CONTENT || status_int / 100 == 1;
233+
body_headers_ignored |= status_int == AWS_HTTP_STATUS_CODE_304_NOT_MODIFIED;
234+
bool body_headers_forbidden = status_int == AWS_HTTP_STATUS_CODE_204_NO_CONTENT || status_int / 100 == 1;
235235
err = s_scan_outgoing_headers(message, response, &header_lines_len, body_headers_ignored, body_headers_forbidden);
236236
if (err) {
237237
goto error;

source/h1_stream.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15-
#include <aws/http/private/h1_stream.h>
16-
1715
#include <aws/http/private/connection_impl.h>
16+
#include <aws/http/private/h1_stream.h>
17+
#include <aws/http/status_code.h>
1818

1919
#include <aws/io/logging.h>
2020

@@ -96,7 +96,7 @@ struct aws_h1_stream *aws_h1_stream_new_request(
9696
}
9797

9898
stream->base.client_data = &stream->base.client_or_server_data.client;
99-
stream->base.client_data->response_status = AWS_HTTP_STATUS_UNKNOWN;
99+
stream->base.client_data->response_status = AWS_HTTP_STATUS_CODE_UNKNOWN;
100100

101101
/* Validate request and cache info that the encoder will eventually need */
102102
int err =

0 commit comments

Comments
 (0)