Browse Source

[core] perf: tighten some code in some hot paths

personal/stbuehler/ci-build
Glenn Strauss 2 years ago
parent
commit
b04f0311f9
  1. 42
      src/buffer.c
  2. 12
      src/connections.c
  3. 7
      src/http_header.c

42
src/buffer.c

@ -723,7 +723,7 @@ void buffer_copy_string_encoded_cgi_varnames(buffer * const restrict b, const ch
* replaces non-printable characters with '_'
*/
static void buffer_urldecode_internal(buffer *url, int is_query) {
static void buffer_urldecode_internal(buffer * const url, const int is_query) {
unsigned char high, low;
char *src;
char *dst;
@ -735,11 +735,17 @@ static void buffer_urldecode_internal(buffer *url, int is_query) {
src = (char*) url->ptr;
while ('\0' != *src) {
if ('%' == *src) break;
if (is_query && '+' == *src) *src = ' ';
src++;
if (!is_query) {
while ('\0' != *src && '%' != *src) ++src;
}
else {
for (; '\0' != *src && '%' != *src; ++src) {
if ('+' == *src) *src = ' ';
}
}
if ('\0' == *src) return;
dst = src;
while ('\0' != *src) {
@ -923,23 +929,21 @@ void buffer_path_simplify(buffer *dest, buffer *src)
buffer_string_set_length(dest, out - start);
}
void buffer_to_lower(buffer *b) {
size_t i;
for (i = 0; i < b->used; ++i) {
char c = b->ptr[i];
if (c >= 'A' && c <= 'Z') b->ptr[i] |= 0x20;
}
void buffer_to_lower(buffer * const b) {
char * const s = b->ptr;
for (uint32_t i = 0; i < b->used; ++i) {
char c = s[i];
if (c >= 'A' && c <= 'Z') s[i] |= 0x20;
}
}
void buffer_to_upper(buffer *b) {
size_t i;
for (i = 0; i < b->used; ++i) {
char c = b->ptr[i];
if (c >= 'a' && c <= 'z') b->ptr[i] &= ~0x20;
}
void buffer_to_upper(buffer * const b) {
char * const s = b->ptr;
for (uint32_t i = 0; i < b->used; ++i) {
char c = s[i];
if (c >= 'a' && c <= 'z') s[i] &= ~0x20;
}
}

12
src/connections.c

@ -404,16 +404,20 @@ static int connection_handle_write_prepare(request_st * const r) {
r->http_status == 304) {
/* no Content-Body, no Content-Length */
http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
} else if (qlen > 0 || r->http_method != HTTP_METHOD_HEAD) {
/* qlen = 0 is important for Redirects (301, ...) as they MAY have
* a content. Browsers are waiting for a Content otherwise
*/
} else if (qlen > 0) {
buffer * const tb = r->tmp_buf;
buffer_clear(tb);
buffer_append_int(tb, qlen);
http_header_response_set(r, HTTP_HEADER_CONTENT_LENGTH,
CONST_STR_LEN("Content-Length"),
CONST_BUF_LEN(tb));
} else if (r->http_method != HTTP_METHOD_HEAD) {
/* qlen = 0 is important for Redirects (301, ...) as they MAY have
* a content. Browsers are waiting for a Content otherwise
*/
http_header_response_set(r, HTTP_HEADER_CONTENT_LENGTH,
CONST_STR_LEN("Content-Length"),
CONST_STR_LEN("0"));
}
}
} else {

7
src/http_header.c

@ -55,11 +55,10 @@ enum http_header_e http_header_hkey_get(const char * const s, const uint32_t sle
const struct keyvlenvalue * const kv = http_headers;
int i = slen < sizeof(http_headers_off) ? http_headers_off[slen] : -1;
if (i < 0) return HTTP_HEADER_OTHER;
for (; slen == kv[i].vlen; ++i) {
if (slen == kv[i].vlen
&& buffer_eq_icase_ssn(s, kv[i].value, slen))
do {
if (buffer_eq_icase_ssn(s, kv[i].value, slen))
return (enum http_header_e)kv[i].key;
}
} while (slen == kv[++i].vlen);
return HTTP_HEADER_OTHER;
}

Loading…
Cancel
Save