Browse Source

[core] http_cgi_encode_varname()

move and rename from buffer.c to http_cgi.c:
buffer_copy_string_encoded_cgi_varnames() -> http_cgi_encode_varname()
master
Glenn Strauss 9 months ago
parent
commit
b9e1e93fb1
  1. 31
      src/buffer.c
  2. 3
      src/buffer.h
  3. 29
      src/http_cgi.c

31
src/buffer.c

@ -671,37 +671,6 @@ void buffer_append_string_c_escaped(buffer * const restrict b, const char * cons
}
void buffer_copy_string_encoded_cgi_varnames(buffer * const restrict b, const char * const restrict s, size_t s_len, int is_http_header) {
size_t i, j = 0;
force_assert(NULL != b);
force_assert(NULL != s || 0 == s_len);
buffer_string_prepare_copy(b, s_len + 5);
if (is_http_header) {
if (s_len == 12 && buffer_eq_icase_ssn(s, "Content-Type", 12)) {
buffer_copy_string_len(b, CONST_STR_LEN("CONTENT_TYPE"));
return;
}
buffer_copy_string_len(b, CONST_STR_LEN("HTTP_"));
j = 5; /* "HTTP_" */
}
for (i = 0; i < s_len; ++i) {
unsigned char cr = s[i];
if (light_isalpha(cr)) {
/* upper-case */
cr &= ~32;
} else if (!light_isdigit(cr)) {
cr = '_';
}
b->ptr[j++] = cr;
}
b->used = j;
b->ptr[b->used++] = '\0';
}
/* decodes url-special-chars inplace.
* replaces non-printable characters with '_'
*/

3
src/buffer.h

@ -172,9 +172,6 @@ void buffer_append_string_encoded(buffer * restrict b, const char * restrict s,
/* escape non-printable characters; simple escapes for \t, \r, \n; fallback to \xCC */
void buffer_append_string_c_escaped(buffer * restrict b, const char * restrict s, size_t s_len);
/* to upper case, replace non alpha-numerics with '_'; if is_http_header prefix with "HTTP_" unless s is "content-type" */
void buffer_copy_string_encoded_cgi_varnames(buffer * restrict b, const char * restrict s, size_t s_len, int is_http_header);
void buffer_urldecode_path(buffer *url);
void buffer_urldecode_query(buffer *url);
int buffer_is_valid_UTF8(const buffer *b);

29
src/http_cgi.c

@ -91,6 +91,29 @@ http_cgi_local_redir (request_st * const r)
}
static void
http_cgi_encode_varname (buffer * const b, const char * const restrict s, const size_t len, const int is_http_header)
{
char * const restrict p = buffer_string_prepare_copy(b, len + 5);
size_t i, j = 0;
if (is_http_header) {
if (len == 12 && buffer_eq_icase_ssn(s, "Content-Type", 12)) {
buffer_copy_string_len(b, CONST_STR_LEN("CONTENT_TYPE"));
return;
}
memcpy(p, "HTTP_", 5);
j = 5; /* "HTTP_" */
}
for (i = 0; i < len; ++i) {/* uppercase alpha, pass numeric, map rest '_' */
const unsigned char c = s[i];
p[j++] = light_isalpha(c) ? c & ~0x20 : light_isdigit(c) ? c : '_';
}
buffer_string_set_length(b, j);
}
int
http_cgi_headers (request_st * const r, http_cgi_opts * const opts, http_cgi_header_append_cb cb, void *vdata)
{
@ -308,8 +331,7 @@ http_cgi_headers (request_st * const r, http_cgi_opts * const opts, http_cgi_hea
&& buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Proxy"))) {
continue;
}
buffer_copy_string_encoded_cgi_varnames(tb,
CONST_BUF_LEN(&ds->key), 1);
http_cgi_encode_varname(tb, CONST_BUF_LEN(&ds->key), 1);
rc |= cb(vdata, CONST_BUF_LEN(tb),
CONST_BUF_LEN(&ds->value));
}
@ -320,8 +342,7 @@ http_cgi_headers (request_st * const r, http_cgi_opts * const opts, http_cgi_hea
for (n = 0; n < r->env.used; n++) {
data_string *ds = (data_string *)r->env.data[n];
if (!buffer_is_empty(&ds->value) && !buffer_is_empty(&ds->key)) {
buffer_copy_string_encoded_cgi_varnames(tb,
CONST_BUF_LEN(&ds->key), 0);
http_cgi_encode_varname(tb, CONST_BUF_LEN(&ds->key), 0);
rc |= cb(vdata, CONST_BUF_LEN(tb),
CONST_BUF_LEN(&ds->value));
}

Loading…
Cancel
Save