Browse Source

Add a li_const_gstring method to create fake GStrings; make some GString* parameters const

personal/stbuehler/wip
Stefan Bühler 12 years ago
parent
commit
e71b64f41f
  1. 2
      include/lighttpd/http_range_parser.h
  2. 20
      include/lighttpd/utils.h
  3. 3
      src/common/angel_data.c
  4. 10
      src/common/utils.c
  5. 7
      src/main/environment.c
  6. 2
      src/main/http_range_parser.rl
  7. 4
      src/main/plugin_core.c
  8. 14
      src/modules/mod_auth.c
  9. 2
      src/modules/mod_fastcgi.c

2
include/lighttpd/http_range_parser.h

@ -24,7 +24,7 @@ typedef enum {
LI_PARSE_HTTP_RANGE_NOT_SATISFIABLE
} liParseHttpRangeResult;
LI_API void li_parse_http_range_init(liParseHttpRangeState* s, GString *range_str, goffset limit);
LI_API void li_parse_http_range_init(liParseHttpRangeState* s, const GString *range_str, goffset limit);
LI_API liParseHttpRangeResult li_parse_http_range_next(liParseHttpRangeState* s);
LI_API void li_parse_http_range_clear(liParseHttpRangeState* s);

20
include/lighttpd/utils.h

@ -45,7 +45,7 @@ LI_API void li_path_simplify(GString *path);
INLINE void li_path_append_slash(GString *path);
/* finds the first value for a given key in the querystring. works with '&' as well as ';' delimiters */
LI_API gboolean li_querystring_find(GString *querystring, const gchar *key, const guint key_len, gchar **val, guint *val_len);
LI_API gboolean li_querystring_find(const GString *querystring, const gchar *key, const guint key_len, gchar **val, guint *val_len);
/* formats a given guint64 for output. if dest is NULL, a new string is allocated */
LI_API GString *li_counter_format(guint64 count, liCounterType t, GString *dest);
@ -62,7 +62,7 @@ LI_API guint li_hash_ipv6(gconstpointer key);
/* converts a sock_addr to a human readable string. ipv4 and ipv6 supported. if dest is NULL, a new string will be allocated */
LI_API GString *li_sockaddr_to_string(liSocketAddress addr, GString *dest, gboolean showport);
LI_API liSocketAddress li_sockaddr_from_string(GString *str, guint tcp_default_port);
LI_API liSocketAddress li_sockaddr_from_string(const GString *str, guint tcp_default_port);
LI_API liSocketAddress li_sockaddr_local_from_socket(gint fd);
LI_API liSocketAddress li_sockaddr_remote_from_socket(gint fd);
LI_API void li_sockaddr_clear(liSocketAddress *saddr);
@ -74,12 +74,14 @@ LI_API gboolean ipv4_in_ipv6_net(guint32 target, const guint8 *match, guint netw
LI_API void li_gstring_replace_char_with_str_len(GString *gstr, gchar c, gchar *str, guint len);
LI_API gboolean li_strncase_equal(GString *str, const gchar *s, guint len);
INLINE GString li_const_gstring(const gchar *str, gsize len);
LI_API gboolean li_strncase_equal(const GString *str, const gchar *s, guint len);
LI_API GString *li_string_assign_len(GString *string, const gchar *val, gssize len);
LI_API gboolean li_string_prefix(GString *str, const gchar *s, gsize len);
LI_API gboolean li_string_suffix(GString *str, const gchar *s, gsize len);
LI_API gboolean li_string_prefix(const GString *str, const gchar *s, gsize len);
LI_API gboolean li_string_suffix(const GString *str, const gchar *s, gsize len);
LI_API void li_string_append_int(GString *dest, gint64 val);
@ -105,7 +107,15 @@ LI_API gboolean _li_set_sys_error(GError **error, const gchar *msg, const gchar
#endif
/* inline implementations */
INLINE void li_path_append_slash(GString *path) {
if (path->len == 0 || path->str[path->len-1] != '/')
g_string_append_len(path, "/", 1);
}
/** warning: This "GString" does not make sure that there is a terminating '\0', and you shouldn't modify the GString */
INLINE GString li_const_gstring(const gchar *str, gsize len) {
GString gs = { (gchar*) str, len, 0 };
return gs;
}

3
src/common/angel_data.c

@ -1,6 +1,7 @@
#include <lighttpd/settings.h>
#include <lighttpd/angel_data.h>
#include <lighttpd/utils.h>
/* error handling */
GQuark li_angel_data_error_quark() {
@ -50,7 +51,7 @@ gboolean li_angel_data_write_str (GString *buf, const GString *str, GError **er
}
gboolean li_angel_data_write_cstr (GString *buf, const gchar *str, gsize len, GError **err) {
const GString tmps = { (gchar*) str, len, 0 }; /* fake const GString */
const GString tmps = li_const_gstring(str, len); /* fake const GString */
return li_angel_data_write_str(buf, &tmps, err);
}

10
src/common/utils.c

@ -374,7 +374,7 @@ void li_path_simplify(GString *path) {
}
gboolean li_querystring_find(GString *querystring, const gchar *key, const guint key_len, gchar **val, guint *val_len) {
gboolean li_querystring_find(const GString *querystring, const gchar *key, const guint key_len, gchar **val, guint *val_len) {
gchar delim = '\0';
gchar *end = querystring->str + querystring->len;
gchar *start = querystring->str;
@ -578,7 +578,7 @@ GString *li_sockaddr_to_string(liSocketAddress addr, GString *dest, gboolean sho
return dest;
}
liSocketAddress li_sockaddr_from_string(GString *str, guint tcp_default_port) {
liSocketAddress li_sockaddr_from_string(const GString *str, guint tcp_default_port) {
guint32 ipv4;
#ifdef HAVE_IPV6
guint8 ipv6[16];
@ -690,19 +690,19 @@ void li_gstring_replace_char_with_str_len(GString *gstr, gchar c, gchar *str, gu
}
}
gboolean li_strncase_equal(GString *str, const gchar *s, guint len) {
gboolean li_strncase_equal(const GString *str, const gchar *s, guint len) {
if (str->len != len) return FALSE;
return 0 == g_ascii_strncasecmp(str->str, s, len);
}
gboolean li_string_suffix(GString *str, const gchar *s, gsize len) {
gboolean li_string_suffix(const GString *str, const gchar *s, gsize len) {
if (str->len < len)
return FALSE;
return (strcmp(str->str + str->len - len, s) == 0);
}
gboolean li_string_prefix(GString *str, const gchar *s, gsize len) {
gboolean li_string_prefix(const GString *str, const gchar *s, gsize len) {
if (str->len < len)
return FALSE;

7
src/main/environment.c

@ -1,5 +1,6 @@
#include <lighttpd/environment.h>
#include <lighttpd/utils.h>
static void _hash_free_gstring(gpointer data) {
g_string_free((GString*) data, TRUE);
@ -36,12 +37,12 @@ void li_environment_insert(liEnvironment *env, const gchar *key, size_t keylen,
}
void li_environment_remove(liEnvironment *env, const gchar *key, size_t keylen) {
const GString skey = { (gchar*) key, keylen, 0 }; /* fake a constant GString */
const GString skey = li_const_gstring(key, keylen); /* fake a constant GString */
g_hash_table_remove(env->table, &skey);
}
GString* li_environment_get(liEnvironment *env, const gchar *key, size_t keylen) {
const GString skey = { (gchar*) key, keylen, 0 }; /* fake a constant GString */
const GString skey = li_const_gstring(key, keylen); /* fake a constant GString */
return (GString*) g_hash_table_lookup(env->table, &skey);
}
@ -65,7 +66,7 @@ void li_environment_dup_free(liEnvironmentDup *envdup) {
}
GString* li_environment_dup_pop(liEnvironmentDup *envdup, const gchar *key, size_t keylen) {
const GString skey = { (gchar*) key, keylen, 0 }; /* fake a constant GString */
const GString skey = li_const_gstring(key, keylen); /* fake a constant GString */
GString *sval = (GString*) g_hash_table_lookup(envdup->table, &skey);
if (sval) g_hash_table_remove(envdup->table, &skey);
return sval;

2
src/main/http_range_parser.rl

@ -83,7 +83,7 @@ liParseHttpRangeResult li_parse_http_range_next(liParseHttpRangeState* s) {
}
}
void li_parse_http_range_init(liParseHttpRangeState* s, GString *range_str, goffset limit) {
void li_parse_http_range_init(liParseHttpRangeState* s, const GString *range_str, goffset limit) {
s->data = g_string_new_len(GSTR_LEN(range_str));
s->data_pos = s->data->str;
s->limit = limit;

4
src/main/plugin_core.c

@ -334,7 +334,7 @@ static liHandlerResult core_handle_static(liVRequest *vr, gpointer param, gpoint
if (exclude_arr) {
const gchar *basep = g_basename(vr->physical.path->str);
GString base = { (gchar*) basep, vr->physical.path->len - (basep - vr->physical.path->str), 0 };
const GString base = li_const_gstring((gchar*) basep, vr->physical.path->len - (basep - vr->physical.path->str));
guint i;
for (i = 0; i < exclude_arr->len; i++) {
@ -431,7 +431,7 @@ static liHandlerResult core_handle_static(liVRequest *vr, gpointer param, gpoint
hh_range = li_http_header_lookup(vr->request.headers, CONST_STR_LEN("range"));
if (hh_range) {
/* TODO: Check If-Range: header */
GString range_str = { HEADER_VALUE_LEN(hh_range), 0 };
const GString range_str = li_const_gstring(HEADER_VALUE_LEN(hh_range));
liParseHttpRangeState rs;
gboolean is_multipart = FALSE, done = FALSE;

14
src/modules/mod_auth.c

@ -166,9 +166,7 @@ static gboolean auth_backend_plain(liVRequest *vr, const gchar *auth_info, gpoin
return FALSE;
}
user.str = decoded;
user.len = c - decoded;
user.allocated_len = 0;
user = li_const_gstring(decoded, c - decoded);
/* unknown user? */
if (!(pass = g_hash_table_lookup(ad->data, &user))) {
@ -252,15 +250,11 @@ static liAction* auth_plain_create(liServer *srv, liPlugin* p, liValue *val) {
return NULL;
}
str.allocated_len = 0;
str.str = "method";
str.len = sizeof("method") - 1;
str = li_const_gstring(CONST_STR_LEN("method"));
method = g_hash_table_lookup(val->data.hash, &str);
str.str = "realm";
str.len = sizeof("realm") - 1;
str = li_const_gstring(CONST_STR_LEN("realm"));
realm = g_hash_table_lookup(val->data.hash, &str);
str.str = "file";
str.len = sizeof("file") - 1;
str = li_const_gstring(CONST_STR_LEN("file"));
file = g_hash_table_lookup(val->data.hash, &str);
if (!method || method->type != LI_VALUE_STRING || !realm || realm->type != LI_VALUE_STRING || !file || file->type != LI_VALUE_STRING) {

2
src/modules/mod_fastcgi.c

@ -431,7 +431,7 @@ static void fastcgi_send_env(liVRequest *vr, fastcgi_connection *fcon) {
for (i = vr->request.headers->entries.head; NULL != i; i = i->next) {
liHttpHeader *h = (liHttpHeader*) i->data;
GString hkey = { h->data->str, h->keylen, 0 };
const GString hkey = li_const_gstring(h->data->str, h->keylen);
g_string_truncate(tmp, 0);
if (!li_strncase_equal(&hkey, CONST_STR_LEN("CONTENT-TYPE"))) {
g_string_append_len(tmp, CONST_STR_LEN("HTTP_"));

Loading…
Cancel
Save