Browse Source

[core] code reuse with array_insert_key_value()

code reuse with array_insert_key_value() and related array manipulation
personal/stbuehler/fix-fdevent
Glenn Strauss 3 years ago
parent
commit
a7c27c9f99
  1. 35
      src/array.c
  2. 4
      src/array.h
  3. 9
      src/connections.c
  4. 29
      src/gw_backend.c
  5. 9
      src/http-header-glue.c
  6. 25
      src/http_auth.c
  7. 10
      src/mod_extforward.c
  8. 9
      src/mod_flv_streaming.c
  9. 107
      src/mod_geoip.c
  10. 8
      src/mod_indexfile.c
  11. 8
      src/mod_magnet.c
  12. 17
      src/mod_openssl.c
  13. 17
      src/mod_proxy.c
  14. 22
      src/mod_setenv.c
  15. 23
      src/mod_ssi.c
  16. 28
      src/request.c
  17. 35
      src/status_counter.c
  18. 9
      src/status_counter.h

35
src/array.c

@ -170,7 +170,7 @@ data_unset *array_extract_element_klen(array *a, const char *key, size_t klen) {
return NULL;
}
data_unset *array_get_unused_element(array *a, data_type_t t) {
static data_unset *array_get_unused_element(array *a, data_type_t t) {
data_unset *ds = NULL;
unsigned int i;
@ -190,20 +190,37 @@ data_unset *array_get_unused_element(array *a, data_type_t t) {
}
void array_set_key_value(array *hdrs, const char *key, size_t key_len, const char *value, size_t val_len) {
data_string *ds_dst;
data_string *ds;
if (NULL != (ds_dst = (data_string *)array_get_element_klen(hdrs, key, key_len))) {
buffer_copy_string_len(ds_dst->value, value, val_len);
if (NULL != (ds = (data_string *)array_get_element_klen(hdrs, key, key_len))) {
buffer_copy_string_len(ds->value, value, val_len);
return;
}
if (NULL == (ds_dst = (data_string *)array_get_unused_element(hdrs, TYPE_STRING))) {
ds_dst = data_string_init();
array_insert_key_value(hdrs, key, key_len, value, val_len);
}
void array_insert_key_value(array *hdrs, const char *key, size_t key_len, const char *value, size_t val_len) {
data_string *ds;
if (NULL == (ds = (data_string *)array_get_unused_element(hdrs, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, key, key_len);
buffer_copy_string_len(ds->value, value, val_len);
array_insert_unique(hdrs, (data_unset *)ds);
}
void array_insert_value(array *hdrs, const char *value, size_t val_len) {
data_string *ds;
if (NULL == (ds = (data_string *)array_get_unused_element(hdrs, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds_dst->key, key, key_len);
buffer_copy_string_len(ds_dst->value, value, val_len);
array_insert_unique(hdrs, (data_unset *)ds_dst);
buffer_copy_string_len(ds->value, value, val_len);
array_insert_unique(hdrs, (data_unset *)ds);
}
/* if entry already exists return pointer to existing entry, otherwise insert entry and return NULL */

4
src/array.h

@ -65,13 +65,13 @@ int array_is_kvany(array *a);
int array_is_kvarray(array *a);
int array_is_kvstring(array *a);
int array_print(array *a, int depth);
data_unset *array_get_unused_element(array *a, data_type_t t);
#define array_get_element(a, key) array_get_element_klen((a), (key), sizeof(key)-1)
data_unset *array_get_element_klen(const array *a, const char *key, size_t klen);
data_unset *array_extract_element_klen(array *a, const char *key, size_t klen); /* removes found entry from array */
void array_set_key_value(array *hdrs, const char *key, size_t key_len, const char *value, size_t val_len);
void array_insert_key_value(array *hdrs, const char *key, size_t key_len, const char *value, size_t val_len);
void array_insert_value(array *hdrs, const char *value, size_t val_len);
void array_replace(array *a, data_unset *entry);
int array_strcasecmp(const char *a, size_t a_len, const char *b, size_t b_len);
void array_print_indent(int depth);
size_t array_get_max_key_length(array *a);

9
src/connections.c

@ -1229,13 +1229,8 @@ int connection_state_machine(server *srv, connection *con) {
/* set REDIRECT_STATUS to save current HTTP status code
* for access by dynamic handlers
* https://redmine.lighttpd.net/issues/1828 */
data_string *ds;
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, CONST_STR_LEN("REDIRECT_STATUS"));
buffer_append_int(ds->value, con->http_status);
array_insert_unique(con->environment, (data_unset *)ds);
buffer_copy_int(srv->tmp_buf, con->http_status);
array_insert_key_value(con->environment, CONST_STR_LEN("REDIRECT_STATUS"), CONST_BUF_LEN(srv->tmp_buf));
if (error_handler == con->conf.error_handler) {
plugins_call_connection_reset(srv, con);

29
src/gw_backend.c

@ -33,7 +33,7 @@
#include "status_counter.h"
static data_integer * gw_status_get_di(server *srv, gw_host *host, gw_proc *proc, const char *tag, size_t len) {
static int * gw_status_get_counter(server *srv, gw_host *host, gw_proc *proc, const char *tag, size_t len) {
buffer *b = srv->tmp_buf;
buffer_copy_string_len(b, CONST_STR_LEN("gw.backend."));
buffer_append_string_buffer(b, host->id);
@ -46,42 +46,37 @@ static data_integer * gw_status_get_di(server *srv, gw_host *host, gw_proc *proc
}
static void gw_proc_tag_inc(server *srv, gw_host *host, gw_proc *proc, const char *tag, size_t len) {
data_integer *di = gw_status_get_di(srv, host, proc, tag, len);
++di->value;
++(*gw_status_get_counter(srv, host, proc, tag, len));
}
static void gw_proc_load_inc(server *srv, gw_host *host, gw_proc *proc) {
data_integer *di = gw_status_get_di(srv,host,proc,CONST_STR_LEN(".load"));
di->value = ++proc->load;
*gw_status_get_counter(srv,host,proc,CONST_STR_LEN(".load")) = ++proc->load;
status_counter_inc(srv, CONST_STR_LEN("gw.active-requests"));
}
static void gw_proc_load_dec(server *srv, gw_host *host, gw_proc *proc) {
data_integer *di = gw_status_get_di(srv,host,proc,CONST_STR_LEN(".load"));
di->value = --proc->load;
*gw_status_get_counter(srv,host,proc,CONST_STR_LEN(".load")) = --proc->load;
status_counter_dec(srv, CONST_STR_LEN("gw.active-requests"));
}
static void gw_host_assign(server *srv, gw_host *host) {
data_integer *di = gw_status_get_di(srv,host,NULL,CONST_STR_LEN(".load"));
di->value = ++host->load;
*gw_status_get_counter(srv,host,NULL,CONST_STR_LEN(".load")) = ++host->load;
}
static void gw_host_reset(server *srv, gw_host *host) {
data_integer *di = gw_status_get_di(srv,host,NULL,CONST_STR_LEN(".load"));
di->value = --host->load;
*gw_status_get_counter(srv,host,NULL,CONST_STR_LEN(".load")) = --host->load;
}
static int gw_status_init(server *srv, gw_host *host, gw_proc *proc) {
gw_status_get_di(srv, host, proc, CONST_STR_LEN(".disabled"))->value = 0;
gw_status_get_di(srv, host, proc, CONST_STR_LEN(".died"))->value = 0;
gw_status_get_di(srv, host, proc, CONST_STR_LEN(".overloaded"))->value = 0;
gw_status_get_di(srv, host, proc, CONST_STR_LEN(".connected"))->value = 0;
gw_status_get_di(srv, host, proc, CONST_STR_LEN(".load"))->value = 0;
*gw_status_get_counter(srv, host, proc, CONST_STR_LEN(".disabled")) = 0;
*gw_status_get_counter(srv, host, proc, CONST_STR_LEN(".died")) = 0;
*gw_status_get_counter(srv, host, proc, CONST_STR_LEN(".overloaded")) = 0;
*gw_status_get_counter(srv, host, proc, CONST_STR_LEN(".connected")) = 0;
*gw_status_get_counter(srv, host, proc, CONST_STR_LEN(".load")) = 0;
gw_status_get_di(srv, host, NULL, CONST_STR_LEN(".load"))->value = 0;
*gw_status_get_counter(srv, host, NULL, CONST_STR_LEN(".load")) = 0;
return 0;
}

9
src/http-header-glue.c

@ -905,7 +905,6 @@ static int http_response_process_headers(server *srv, connection *con, http_resp
for (s = hdrs->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1, ++line) {
const char *key, *value;
int key_len;
data_string *ds;
/* strip the \n */
ns[0] = '\0';
@ -955,13 +954,7 @@ static int http_response_process_headers(server *srv, connection *con, http_resp
}
} else if (key_len > 9
&& 0==strncasecmp(key, CONST_STR_LEN("Variable-"))) {
ds = (data_string *)
array_get_unused_element(con->environment, TYPE_STRING);
if (NULL == ds) ds = data_string_init();
buffer_copy_string_len(ds->key, key + 9, key_len - 9);
buffer_copy_string(ds->value, value);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, key + 9, key_len - 9, value, strlen(value));
}
continue;
}

25
src/http_auth.c

@ -132,29 +132,8 @@ int http_auth_match_rules (const http_auth_require_t * const require, const char
}
void http_auth_setenv(array *env, const char *username, size_t ulen, const char *auth_type, size_t alen) {
data_string *ds;
/* REMOTE_USER */
if (NULL == (ds = (data_string *)array_get_element(env, "REMOTE_USER"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(env, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, CONST_STR_LEN("REMOTE_USER"));
array_insert_unique(env, (data_unset *)ds);
}
buffer_copy_string_len(ds->value, username, ulen);
/* AUTH_TYPE */
if (NULL == (ds = (data_string *)array_get_element(env, "AUTH_TYPE"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(env, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, CONST_STR_LEN("AUTH_TYPE"));
array_insert_unique(env, (data_unset *)ds);
}
buffer_copy_string_len(ds->value, auth_type, alen);
array_set_key_value(env, CONST_STR_LEN("REMOTE_USER"), username, ulen);
array_set_key_value(env, CONST_STR_LEN("AUTH_TYPE"), auth_type, alen);
}
int http_auth_md5_hex2bin (const char *md5hex, size_t len, unsigned char md5bin[16])

10
src/mod_extforward.c

@ -977,13 +977,8 @@ static handler_t mod_extforward_Forwarded (server *srv, connection *con, plugin_
&& NULL == array_get_element(con->request.headers, "X-Forwarded-For")) {
/* create X-Forwarded-For if not present
* (and at least original connecting IP is a trusted proxy) */
buffer *xff;
data_string *dsxff = (data_string *)
array_get_unused_element(con->request.headers, TYPE_STRING);
if (NULL == dsxff) dsxff = data_string_init();
buffer_copy_string_len(dsxff->key, CONST_STR_LEN("X-Forwarded-For"));
array_insert_unique(con->request.headers, (data_unset *)dsxff);
xff = dsxff->value;
buffer *xff = srv->tmp_buf;
buffer_string_set_length(xff, 0);
for (j = 0; j < used; ) {
if (-1 == offsets[j]) { ++j; continue; }
if (3 == offsets[j+1]
@ -1014,6 +1009,7 @@ static handler_t mod_extforward_Forwarded (server *srv, connection *con, plugin_
}
j += 4; /*(k, klen, v, vlen come in sets of 4)*/
}
array_insert_key_value(con->request.headers, CONST_STR_LEN("X-Forwarded-For"), CONST_BUF_LEN(xff));
}
#endif

9
src/mod_flv_streaming.c

@ -165,19 +165,12 @@ static int split_get_params(array *get_params, buffer *qrystr) {
case '&':
case '\0': /* fin symbol */
if (!is_key) {
data_string *ds;
/* we need at least a = since the last & */
/* terminate the value */
qrystr->ptr[i] = '\0';
if (NULL == (ds = (data_string *)array_get_unused_element(get_params, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, key, strlen(key));
buffer_copy_string_len(ds->value, val, strlen(val));
array_insert_unique(get_params, (data_unset *)ds);
array_insert_key_value(get_params, key, strlen(key), val, strlen(val));
}
key = qrystr->ptr + i + 1;

107
src/mod_geoip.c

@ -215,47 +215,30 @@ static handler_t mod_geoip_query (connection *con, plugin_data *p) {
const char *remote_ip;
data_string *ds;
GeoIPRecord *gir;
const char *returnedCountry;
remote_ip = con->dst_addr_buf->ptr;
if (p->conf.gi->databaseType == GEOIP_COUNTRY_EDITION) {
const char *returnedCountry;
/* get the country code 2 chars */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
if (NULL != (returnedCountry = GeoIP_country_code_by_addr(p->conf.gi, remote_ip))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
buffer_copy_string(ds->value, returnedCountry);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE"), returnedCountry, strlen(returnedCountry));
}
}
/* get the country code 3 chars */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
if (NULL != (returnedCountry = GeoIP_country_code3_by_addr(p->conf.gi, remote_ip))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
buffer_copy_string(ds->value, returnedCountry);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE3"), returnedCountry, strlen(returnedCountry));
}
}
/* get the country name */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
if (NULL != (returnedCountry = GeoIP_country_name_by_addr(p->conf.gi, remote_ip))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
buffer_copy_string(ds->value, returnedCountry);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_NAME"), returnedCountry, strlen(returnedCountry));
}
}
@ -268,120 +251,60 @@ static handler_t mod_geoip_query (connection *con, plugin_data *p) {
if (NULL != (gir = GeoIP_record_by_addr(p->conf.gi, remote_ip))) {
/* get the country code 2 chars */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
buffer_copy_string(ds->value, gir->country_code);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE"), gir->country_code, strlen(gir->country_code));
}
/* get the country code 3 chars */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
buffer_copy_string(ds->value, gir->country_code3);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_CODE3"), gir->country_code3, strlen(gir->country_code3));
}
/* get the country name */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
buffer_copy_string(ds->value, gir->country_name);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_COUNTRY_NAME"), gir->country_name, strlen(gir->country_name));
}
/* get the city region */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_REGION"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_CITY_REGION");
buffer_copy_string(ds->value, gir->region);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_CITY_REGION"), gir->region, strlen(gir->region));
}
/* get the city */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_NAME"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_CITY_NAME");
buffer_copy_string(ds->value, gir->city);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_CITY_NAME"), gir->city, strlen(gir->city));
}
/* get the postal code */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_POSTAL_CODE"))) {
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, "GEOIP_CITY_POSTAL_CODE");
buffer_copy_string(ds->value, gir->postal_code);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_CITY_POSTAL_CODE"), gir->postal_code, strlen(gir->postal_code));
}
/* get the latitude */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LATITUDE"))) {
char latitude[32];
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
snprintf(latitude, sizeof(latitude), "%f", gir->latitude);
buffer_copy_string(ds->key, "GEOIP_CITY_LATITUDE");
buffer_copy_string(ds->value, latitude);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_CITY_LATITUDE"), latitude, strlen(latitude));
}
/* get the long latitude */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LONG_LATITUDE"))) {
char long_latitude[32];
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
snprintf(long_latitude, sizeof(long_latitude), "%f", gir->longitude);
buffer_copy_string(ds->key, "GEOIP_CITY_LONG_LATITUDE");
buffer_copy_string(ds->value, long_latitude);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_CITY_LONG_LATITUDE"), long_latitude, strlen(long_latitude));
}
/* get the dma code */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_DMA_CODE"))) {
char dc[5];
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
snprintf(dc, sizeof(dc), "%i", gir->dma_code);
buffer_copy_string(ds->key, "GEOIP_CITY_DMA_CODE");
buffer_copy_string(ds->value, dc);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_CITY_DMA_CODE"), dc, strlen(dc));
}
/* get the area code */
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_AREA_CODE"))) {
char ac[5];
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
snprintf(ac, sizeof(ac), "%i", gir->area_code);
buffer_copy_string(ds->key, "GEOIP_CITY_AREA_CODE");
buffer_copy_string(ds->value, ac);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("GEOIP_CITY_AREA_CODE"), ac, strlen(ac));
}
GeoIPRecord_delete(gir);

8
src/mod_indexfile.c

@ -201,13 +201,7 @@ URIHANDLER_FUNC(mod_indexfile_subrequest) {
if (ds->value && ds->value->ptr[0] == '/') {
/* replace uri.path */
buffer_copy_buffer(con->uri.path, ds->value);
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, CONST_STR_LEN("PATH_TRANSLATED_DIRINDEX"));
buffer_copy_buffer(ds->value, con->physical.path);
array_insert_unique(con->environment, (data_unset *)ds);
array_insert_key_value(con->environment, CONST_STR_LEN("PATH_TRANSLATED_DIRINDEX"), CONST_BUF_LEN(con->physical.path));
} else {
/* append to uri.path the relative path to index file (/ -> /index.php) */
buffer_append_string_buffer(con->uri.path, ds->value);

8
src/mod_magnet.c

@ -431,15 +431,13 @@ static int magnet_reqhdr_pairs(lua_State *L) {
}
static int magnet_status_get(lua_State *L) {
data_integer *di;
int *i;
server *srv = magnet_get_server(L);
/* __index: param 1 is the (empty) table the value was not found in */
const_buffer key = magnet_checkconstbuffer(L, 2);
di = status_counter_get_counter(srv, key.ptr, key.len);
lua_pushinteger(L, (lua_Integer)di->value);
i = status_counter_get_counter(srv, key.ptr, key.len);
lua_pushinteger(L, (lua_Integer)*i);
return 1;
}

17
src/mod_openssl.c

@ -1736,24 +1736,17 @@ https_add_ssl_client_entries (server *srv, connection *con, handler_ctx *hctx)
if (hctx->conf.ssl_verifyclient_export_cert) {
BIO *bio;
if (NULL != (bio = BIO_new(BIO_s_mem()))) {
data_string *envds;
buffer *cert = srv->tmp_buf;
int n;
PEM_write_bio_X509(bio, xs);
n = BIO_pending(bio);
envds = (data_string *)
array_get_unused_element(con->environment, TYPE_STRING);
if (NULL == envds) {
envds = data_string_init();
}
buffer_copy_string_len(envds->key,CONST_STR_LEN("SSL_CLIENT_CERT"));
buffer_string_prepare_copy(envds->value, n);
BIO_read(bio, envds->value->ptr, n);
buffer_string_prepare_copy(cert, n);
BIO_read(bio, cert->ptr, n);
BIO_free(bio);
buffer_commit(envds->value, n);
array_replace(con->environment, (data_unset *)envds);
buffer_commit(cert, n);
array_set_key_value(con->environment, CONST_STR_LEN("SSL_CLIENT_CERT"), CONST_BUF_LEN(cert));
}
}
X509_free(xs);

17
src/mod_proxy.c

@ -494,15 +494,7 @@ static void http_header_remap_setcookie (buffer *b, size_t off, http_header_rema
static void proxy_append_header(connection *con, const char *key, const size_t klen, const char *value, const size_t vlen) {
data_string *ds_dst;
if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
ds_dst = data_string_init();
}
buffer_copy_string_len(ds_dst->key, key, klen);
buffer_copy_string_len(ds_dst->value, value, vlen);
array_insert_unique(con->request.headers, (data_unset *)ds_dst);
array_insert_key_value(con->request.headers, key, klen, value, vlen);
}
static void buffer_append_string_backslash_escaped(buffer *b, const char *s, size_t len) {
@ -545,11 +537,10 @@ static void proxy_set_Forwarded(connection *con, const unsigned int flags) {
if (flags && NULL == ds) {
data_string *xff;
array_insert_key_value(con->request.headers, CONST_STR_LEN("Forwarded"),
CONST_STR_LEN(""));
ds = (data_string *)
array_get_unused_element(con->request.headers, TYPE_STRING);
if (NULL == ds) ds = data_string_init();
buffer_copy_string_len(ds->key, CONST_STR_LEN("Forwarded"));
array_insert_unique(con->request.headers, (data_unset *)ds);
array_get_element(con->request.headers, "Forwarded");
xff = (data_string *)
array_get_element(con->request.headers, "X-Forwarded-For");
if (NULL != xff && !buffer_string_is_empty(xff->value)) {

22
src/mod_setenv.c

@ -221,16 +221,7 @@ URIHANDLER_FUNC(mod_setenv_uri_handler) {
for (k = 0; k < p->conf.request_header->used; k++) {
data_string *ds = (data_string *)p->conf.request_header->data[k];
data_string *ds_dst;
if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
ds_dst = data_string_init();
}
buffer_copy_buffer(ds_dst->key, ds->key);
buffer_copy_buffer(ds_dst->value, ds->value);
array_insert_unique(con->request.headers, (data_unset *)ds_dst);
array_insert_key_value(con->request.headers, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
}
for (k = 0; k < hctx->conf.set_request_header->used; ++k) {
@ -251,16 +242,7 @@ CONNECTION_FUNC(mod_setenv_handle_request_env) {
for (size_t k = 0; k < hctx->conf.environment->used; ++k) {
data_string *ds = (data_string *)hctx->conf.environment->data[k];
data_string *ds_dst;
if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
ds_dst = data_string_init();
}
buffer_copy_buffer(ds_dst->key, ds->key);
buffer_copy_buffer(ds_dst->value, ds->value);
array_insert_unique(con->environment, (data_unset *)ds_dst);
array_insert_key_value(con->environment, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
}
for (size_t k = 0; k < hctx->conf.set_environment->used; ++k) {

23
src/mod_ssi.c

@ -155,18 +155,7 @@ SETDEFAULTS_FUNC(mod_ssi_set_defaults) {
static int ssi_env_add(void *venv, const char *key, size_t klen, const char *val, size_t vlen) {
array *env = venv;
data_string *ds;
/* array_set_key_value() w/o extra lookup to see if key already exists */
if (NULL == (ds = (data_string *)array_get_unused_element(env, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, key, klen);
buffer_copy_string_len(ds->value, val, vlen);
array_insert_unique(env, (data_unset *)ds);
array_insert_key_value((array *)venv, key, klen, val, vlen);
return 0;
}
@ -691,15 +680,7 @@ static int process_ssi_stmt(server *srv, connection *con, handler_ctx *p, const
if (p->if_is_false) break;
if (key && val) {
data_string *ds;
if (NULL == (ds = (data_string *)array_get_unused_element(p->ssi_vars, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string(ds->key, key);
buffer_copy_string(ds->value, val);
array_insert_unique(p->ssi_vars, (data_unset *)ds);
array_insert_key_value(p->ssi_vars, key, strlen(key), val, strlen(val));
} else if (key || val) {
log_error_write(srv, __FILE__, __LINE__, "sSSss",
"ssi: var and value have to be set in <!--#set", l[1], "=", l[2], "-->");

28
src/request.c

@ -371,13 +371,7 @@ static int http_request_split_value(array *vals, const char *current, size_t len
break;
case ',':
case '\0': /* end of string also marks the end of a token */
if (NULL == (ds = (data_string *)array_get_unused_element(vals, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->value, token_start, token_end-token_start+1);
array_insert_unique(vals, (data_unset *)ds);
array_insert_value(vals, token_start, token_end-token_start+1);
state = 0;
break;
default:
@ -435,7 +429,6 @@ static void init_parse_header_state(parse_header_state* state) {
*/
static int parse_single_header(server *srv, connection *con, parse_header_state *state, char *k, size_t klen, char *v, size_t vlen) {
int cmp = 1;
data_string *dsh;
const char **save = NULL;
buffer **saveb = NULL;
@ -571,12 +564,7 @@ static int parse_single_header(server *srv, connection *con, parse_header_state
}
}
if (NULL == (dsh = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
dsh = data_string_init();
}
buffer_copy_string_len(dsh->key, k, klen);
buffer_copy_string_len(dsh->value, v, vlen);
array_insert_unique(con->request.headers, (data_unset *)dsh);
array_insert_key_value(con->request.headers, k, klen, v, vlen);
if (save) {
data_string *ds = (data_string *)array_get_element_klen(con->request.headers, k, klen);
@ -886,16 +874,8 @@ int http_request_parse(server *srv, connection *con) {
if (state.reqline_host) {
/* Insert as host header */
data_string *ds;
if (NULL == (ds = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
ds = data_string_init();
}
buffer_copy_string_len(ds->key, CONST_STR_LEN("Host"));
buffer_copy_string_len(ds->value, state.reqline_host, state.reqline_hostlen);
array_insert_unique(con->request.headers, (data_unset *)ds);
con->request.http_host = ds->value;
array_insert_key_value(con->request.headers, CONST_STR_LEN("Host"), state.reqline_host, state.reqline_hostlen);
con->request.http_host = ((data_string *)array_get_element_klen(con->request.headers, CONST_STR_LEN("Host")))->value;
}
if (con->parse_request->ptr[i] == ' ' || con->parse_request->ptr[i] == '\t') {

35
src/status_counter.c

@ -17,46 +17,31 @@
* fastcgi.backend.<key>.disconnects = ...
*/
data_integer *status_counter_get_counter(server *srv, const char *s, size_t len) {
int *status_counter_get_counter(server *srv, const char *s, size_t len) {
data_integer *di;
if (NULL == (di = (data_integer *)array_get_element_klen(srv->status, s, len))) {
/* not found, create it */
if (NULL == (di = (data_integer *)array_get_unused_element(srv->status, TYPE_INTEGER))) {
di = data_integer_init();
}
di = data_integer_init();
buffer_copy_string_len(di->key, s, len);
di->value = 0;
array_insert_unique(srv->status, (data_unset *)di);
}
return di;
return &di->value;
}
/* dummies of the statistic framework functions
* they will be moved to a statistics.c later */
int status_counter_inc(server *srv, const char *s, size_t len) {
data_integer *di = status_counter_get_counter(srv, s, len);
di->value++;
return 0;
void status_counter_inc(server *srv, const char *s, size_t len) {
++(*status_counter_get_counter(srv, s, len));
}
int status_counter_dec(server *srv, const char *s, size_t len) {
data_integer *di = status_counter_get_counter(srv, s, len);
if (di->value > 0) di->value--;
return 0;
void status_counter_dec(server *srv, const char *s, size_t len) {
int *i = status_counter_get_counter(srv, s, len);
if (*i > 0) --(*i);
}
int status_counter_set(server *srv, const char *s, size_t len, int val) {
data_integer *di = status_counter_get_counter(srv, s, len);
di->value = val;
return 0;
void status_counter_set(server *srv, const char *s, size_t len, int val) {
*status_counter_get_counter(srv, s, len) = val;
}

9
src/status_counter.h

@ -3,11 +3,10 @@
#include "first.h"
#include "base_decls.h"
#include "array.h"
data_integer *status_counter_get_counter(server *srv, const char *s, size_t len);
int status_counter_inc(server *srv, const char *s, size_t len);
int status_counter_dec(server *srv, const char *s, size_t len);
int status_counter_set(server *srv, const char *s, size_t len, int val);
int *status_counter_get_counter(server *srv, const char *s, size_t len);
void status_counter_inc(server *srv, const char *s, size_t len);
void status_counter_dec(server *srv, const char *s, size_t len);
void status_counter_set(server *srv, const char *s, size_t len, int val);
#endif

Loading…
Cancel
Save