Browse Source

[core] prefer memchr() over strchr()

personal/stbuehler/ci-build
Glenn Strauss 3 years ago
parent
commit
fca9e5a0b1
  1. 2
      src/connections.c
  2. 13
      src/request.c
  3. 10
      src/response.c

2
src/connections.c

@ -700,7 +700,7 @@ static void connection_read_header(server *srv, connection *con) {
if (n[0] == '\0') { hlen += n - b; continue; }
le = 0;
}
for (; (n = strchr(n, '\n')); ++n) {
for (const char * const end = b+clen; (n = memchr(n,'\n',end-n)); ++n) {
if (n[1] == '\r') ++n;
if (n[1] == '\n') { hlen += n - b + 2; break; }
if (n[1] == '\0') { n = NULL; le = 1; break; }

13
src/request.c

@ -636,28 +636,27 @@ static size_t http_request_parse_reqline(server *srv, connection *con, buffer *h
return http_request_header_line_invalid(srv, 400, "unknown protocol -> 400");
}
*(proto - 1) = '\0'; /*(terminate for strchr())*/
jlen = (size_t)(proto - uri - 1);
if (*uri == '/') {
/* (common case) */
buffer_copy_string_len(con->request.uri, uri, proto - uri - 1);
buffer_copy_string_len(con->request.uri, uri, jlen);
} else if (0 == buffer_caseless_compare(uri, 7, "http://", 7) &&
NULL != (nuri = strchr(uri + 7, '/'))) {
NULL != (nuri = memchr(uri + 7, '/', jlen-7))) {
state->reqline_host = uri + 7;
state->reqline_hostlen = nuri - state->reqline_host;
buffer_copy_string_len(con->request.uri, nuri, proto - nuri - 1);
} else if (0 == buffer_caseless_compare(uri, 8, "https://", 8) &&
NULL != (nuri = strchr(uri + 8, '/'))) {
NULL != (nuri = memchr(uri + 8, '/', jlen-8))) {
state->reqline_host = uri + 8;
state->reqline_hostlen = nuri - state->reqline_host;
buffer_copy_string_len(con->request.uri, nuri, proto - nuri - 1);
} else if (!http_header_strict
|| (HTTP_METHOD_CONNECT == con->request.http_method && (uri[0] == ':' || light_isdigit(uri[0])))
|| (HTTP_METHOD_OPTIONS == con->request.http_method && uri[0] == '*' && uri[1] == '\0')) {
/* everything looks good so far */
buffer_copy_string_len(con->request.uri, uri, proto - uri - 1);
|| (HTTP_METHOD_OPTIONS == con->request.http_method && uri[0] == '*' && 1 == jlen)) {
buffer_copy_string_len(con->request.uri, uri, jlen);
} else {
return http_request_header_line_invalid(srv, 400, "request-URI parse error -> 400");
}

10
src/response.c

@ -374,9 +374,13 @@ handler_t http_response_prepare(server *srv, connection *con) {
}
#endif
} else {
qstr = strchr(con->request.uri->ptr, '#');/* discard fragment */
if (qstr) buffer_string_set_length(con->request.uri, qstr - con->request.uri->ptr);
qstr = strchr(con->request.uri->ptr, '?');
size_t rlen = buffer_string_length(con->request.uri);
qstr = memchr(con->request.uri->ptr, '#', rlen);/* discard fragment */
if (qstr) {
rlen = (size_t)(qstr - con->request.uri->ptr);
buffer_string_set_length(con->request.uri, rlen);
}
qstr = memchr(con->request.uri->ptr, '?', rlen);
}
/** extract query string from request.uri */

Loading…
Cancel
Save