Skip to content

Commit

Permalink
fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman committed Feb 16, 2024
1 parent 011c6eb commit 0a41fbe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 69 deletions.
85 changes: 24 additions & 61 deletions ext-src/php_swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ struct Response {
};

struct ByteBuffer {
const char *_protocol = nullptr;
const char *_status = nullptr;
const char *_reason = nullptr;
const char *protocol = nullptr;
const char *status = nullptr;
const char *reason = nullptr;

size_t http_status_length = 0;
size_t http_headers_length = 0;
Expand All @@ -131,23 +131,15 @@ struct ByteBuffer {
lengths = _lengths;
}

inline void add_status(int status, const char *reason) {
char buf[16];
int length = swoole_itoa(buf, status);
buf[length] = '\0';
add_status(buf, reason);
}

inline void add_status(const char *status, const char *reason) {
_protocol = "HTTP/1.1 ";
_status = status;
_reason = reason;

inline void add_status(const char *_status, const char *_reason) {
protocol = "HTTP/1.1 ";
status = _status;
reason = _reason;
// calculate http status line length
if (!_reason) {
http_status_length = strlen(_protocol) + strlen(_status) + SW_CRLF_LEN;
if (!reason) {
http_status_length = strlen(protocol) + strlen(status) + SW_CRLF_LEN;
} else {
http_status_length = strlen(_protocol) + strlen(_status) + strlen(_reason) + SW_CRLF_LEN + 1;
http_status_length = strlen(protocol) + strlen(status) + strlen(reason) + SW_CRLF_LEN + 1;
}
}

Expand All @@ -159,21 +151,26 @@ struct ByteBuffer {
headers[index] = value;
lengths[index] = value_length;
index++;
// calculate http response header length, 2 => strlen(": ")
http_headers_length += key_length + value_length + SW_CRLF_LEN + 2;

if (value) {
http_headers_length += key_length + value_length + SW_CRLF_LEN + 2;
} else {
// When the value is a nullptr, it means that this response header has a fixed value.
http_headers_length += key_length;
}
}

inline size_t get_protocol_length(size_t length = 0) {
// calculate http protocol length
return http_status_length + http_headers_length + length + SW_CRLF_LEN;
}

inline void write_protocol(String *http_buffer, const char *data, size_t length) {
http_buffer->append(_protocol, strlen(_protocol));
http_buffer->append(_status, strlen(_status));
if (_reason) {
void write_protocol(String *http_buffer, const char *data, size_t length) {
http_buffer->append(protocol, strlen(protocol));
http_buffer->append(status, strlen(status));
if (reason) {
http_buffer->append(" ", 1);
http_buffer->append(_reason, strlen(_reason));
http_buffer->append(reason, strlen(reason));
}
http_buffer->append(SW_CRLF, SW_CRLF_LEN);

Expand All @@ -188,42 +185,9 @@ struct ByteBuffer {
value_length = lengths[i + 1];
key = headers[i++];
value = headers[i++];

if (SW_STRCASEEQ(key, key_length, "Content-Type")) {
if (SW_STRCASEEQ(value, value_length, SW_HTTP_TEXT_PLAIN)) {
http_buffer->append(ZEND_STRL("Content-Type: " SW_HTTP_TEXT_PLAIN "\r\n"));
continue;
}

if (SW_STRCASEEQ(value, value_length, SW_HTTP_DEFAULT_CONTENT_TYPE)) {
http_buffer->append(ZEND_STRL("Content-Type: " SW_HTTP_DEFAULT_CONTENT_TYPE "\r\n"));
continue;
}

if (SW_STRCASEEQ(value, value_length, SW_HTTP_APPLICATION_JSON)) {
http_buffer->append(ZEND_STRL("Content-Type: " SW_HTTP_APPLICATION_JSON "\r\n"));
continue;
}
}

if (SW_STRCASEEQ(key, key_length, "Server") &&
SW_STRCASEEQ(value, value_length, SW_HTTP_SERVER_SOFTWARE)) {
http_buffer->append(ZEND_STRL("Server: " SW_HTTP_SERVER_SOFTWARE "\r\n"));
continue;
}

if (SW_STRCASEEQ(key, key_length, "Transfer-Encoding") &&
SW_STRCASEEQ(value, value_length, "chunked")) {
http_buffer->append(ZEND_STRL("Transfer-Encoding: chunked\r\n"));
continue;
}

if (SW_STRCASEEQ(key, key_length, "Connection")) {
if (SW_STRCASEEQ(value, value_length, "keep-alive")) {
http_buffer->append(ZEND_STRL("Connection: keep-alive\r\n"));
} else {
http_buffer->append(ZEND_STRL("Connection: close\r\n"));
}
if (value == nullptr) {
http_buffer->append(key, key_length);
continue;
}

Expand All @@ -237,7 +201,6 @@ struct ByteBuffer {
if (data) {
http_buffer->append(data, length);
}
assert(http_buffer->length == total);
}
};

Expand Down
22 changes: 14 additions & 8 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,14 @@ void HttpContext::build_header(const char *body, size_t length) {
ByteBuffer http_byte_buffer(headers, lengths);

// http status line
response.reason ? http_byte_buffer.add_status(response.status, response.reason)
: http_byte_buffer.add_status(HttpServer::get_status_message(response.status), nullptr);
char status_to_string[16];
if (!response.reason) {
http_byte_buffer.add_status(HttpServer::get_status_message(response.status), nullptr);
} else {
int length = swoole_itoa(status_to_string, response.status);
status_to_string[length] = '\0';
http_byte_buffer.add_status(status_to_string, response.reason);
}

// http header
uint32_t header_flags = 0x0;
Expand Down Expand Up @@ -430,7 +436,7 @@ void HttpContext::build_header(const char *body, size_t length) {

// http Server Name
if (!(header_flags & HTTP_HEADER_SERVER)) {
http_byte_buffer.add_header(ZEND_STRL("Server"), ZEND_STRL(SW_HTTP_SERVER_SOFTWARE));
http_byte_buffer.add_header(ZEND_STRL("Server: " SW_HTTP_SERVER_SOFTWARE "\r\n"), nullptr, 0);
}

// http Date
Expand All @@ -449,20 +455,20 @@ void HttpContext::build_header(const char *body, size_t length) {

// http Connection
if (!(header_flags & HTTP_HEADER_CONNECTION)) {
keepalive ? http_byte_buffer.add_header(ZEND_STRL("Connection"), ZEND_STRL("keep-alive"))
: http_byte_buffer.add_header(ZEND_STRL("Connection"), ZEND_STRL("close"));
keepalive ? http_byte_buffer.add_header(ZEND_STRL("Connection: keep-alive\r\n"), nullptr, 0)
: http_byte_buffer.add_header(ZEND_STRL("Connection: close\r\n"), nullptr, 0);
}

// http Content-Type
if (!(header_flags & HTTP_HEADER_CONTENT_TYPE)) {
http_byte_buffer.add_header(ZEND_STRL("Content-Type"), ZEND_STRL(SW_HTTP_DEFAULT_CONTENT_TYPE));
http_byte_buffer.add_header(ZEND_STRL("Content-Type: " SW_HTTP_DEFAULT_CONTENT_TYPE "\r\n"), nullptr, 0);
}

// http Chunk
if (send_chunked) {
SW_ASSERT(length == 0);
if (!(header_flags & HTTP_HEADER_TRANSFER_ENCODING)) {
http_byte_buffer.add_header(ZEND_STRL("Transfer-Encoding"), ZEND_STRL("chunked"));
http_byte_buffer.add_header(ZEND_STRL("Transfer-Encoding: chunked\r\n"), nullptr, 0);
}
}

Expand All @@ -475,8 +481,8 @@ void HttpContext::build_header(const char *body, size_t length) {
http_byte_buffer.add_header(ZEND_STRL("Content-Encoding"), content_encoding, strlen(content_encoding));
}
#endif
char content_length[25];
if (!(header_flags & HTTP_HEADER_CONTENT_LENGTH)) {
char content_length[25];
int convert_result = swoole_itoa(content_length, length);
content_length[convert_result] = '\0';
http_byte_buffer.add_header(ZEND_STRL("Content-Length"), content_length, convert_result);
Expand Down

0 comments on commit 0a41fbe

Please sign in to comment.