From d119f69edc7c2a9ad2ded2be7fe71eae89926f92 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Wed, 9 Feb 2011 01:50:10 -0600 Subject: [PATCH] Genericize and extend url_info The url_info struct's author member was too small to snarf 64-byte tags, so that field was renamed to 'name' and extended. parse_url_info also now checks the maximum length. --- cgi/cgi_blerg.c | 22 +++++++++++----------- cgi/rss.c | 6 +++--- common/app.c | 8 +++++--- common/app.h | 5 +++-- database/tags.c | 4 ++-- database/util.c | 15 ++++++++++++--- database/util.h | 1 + http/http_blerg.c | 18 +++++++++--------- 8 files changed, 46 insertions(+), 33 deletions(-) diff --git a/cgi/cgi_blerg.c b/cgi/cgi_blerg.c index 9974513..b6dcb3a 100644 --- a/cgi/cgi_blerg.c +++ b/cgi/cgi_blerg.c @@ -110,17 +110,17 @@ int main(int argc, char *argv[]) { } ret = parse_url_info(path + 5, &info); - if ((ret & URL_INFO_AUTHOR) == 0) { + if ((ret & URL_INFO_NAME) == 0) { respond_404(); exit(0); } - if (!blerg_exists(info.author)) { + if (!blerg_exists(info.name)) { respond_404(); exit(0); } - struct blerg *b = blerg_open(info.author); + struct blerg *b = blerg_open(info.name); if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) { respond_for_range(b, info.record, info.record_to); @@ -157,20 +157,20 @@ int main(int argc, char *argv[]) { } ret = parse_url_info(path + 5, &info); - if ((ret & URL_INFO_AUTHOR) == 0) { + if ((ret & URL_INFO_NAME) == 0) { respond_404(); exit(0); } - if (info.author[0] == 'H') - info.author[0] = '#'; - if (!tag_exists(info.author)) { + if (info.name[0] == 'H') + info.name[0] = '#'; + if (!tag_exists(info.name)) { respond_404(); exit(0); } int recs = 50; - struct tag *taglist = tag_list(info.author, 0, &recs, -1); + struct tag *taglist = tag_list(info.name, 0, &recs, -1); if (recs == 0) { respond_simple_data("[]", 2); @@ -227,17 +227,17 @@ int main(int argc, char *argv[]) { } ret = parse_url_info(path + 6, &info); - if ((ret & URL_INFO_AUTHOR) == 0) { + if ((ret & URL_INFO_NAME) == 0) { respond_404(); exit(0); } - if (!blerg_exists(info.author)) { + if (!blerg_exists(info.name)) { respond_404(); exit(0); } - struct blerg *b = blerg_open(info.author); + struct blerg *b = blerg_open(info.name); uint64_t record_count = blerg_get_record_count(b); blerg_close(b); diff --git a/cgi/rss.c b/cgi/rss.c index c35e33b..f143e55 100644 --- a/cgi/rss.c +++ b/cgi/rss.c @@ -90,18 +90,18 @@ int main (int argc, char *argv) { } ret = parse_url_info(path + 1, &info); - if ((ret & URL_INFO_AUTHOR) == 0) { + if ((ret & URL_INFO_NAME) == 0) { respond_404(); exit(0); } - if (!blerg_exists(info.author)) { + if (!blerg_exists(info.name)) { respond_404(); exit(0); } printf("Content-type: application/rss+xml\r\n\r\n"); - fprint_rss(stdout, info.author); + fprint_rss(stdout, info.name); } diff --git a/common/app.c b/common/app.c index 27dfb5f..55ec75a 100644 --- a/common/app.c +++ b/common/app.c @@ -19,9 +19,11 @@ int parse_url_info(const char *url, struct url_info *info) { } if (len == 0) return 0; - memcpy(info->author, url, len); - info->author[len] = 0; - info->contents |= URL_INFO_AUTHOR; + if (len > MAX_TAG_LENGTH) + len = MAX_TAG_LENGTH; + memcpy(info->name, url, len); + info->name[len] = 0; + info->contents |= URL_INFO_NAME; if (c == NULL || c[1] == 0) return info->contents; diff --git a/common/app.h b/common/app.h index d1df688..b45f472 100644 --- a/common/app.h +++ b/common/app.h @@ -6,6 +6,7 @@ #include #include "database.h" +#include "config.h" #define REALM "Blerg" @@ -15,14 +16,14 @@ #define JSON_SUCCESS "{\"status\": \"success\"}" #define JSON_FAILURE "{\"status\": \"failure\"}" -#define URL_INFO_AUTHOR 0x1 +#define URL_INFO_NAME 0x1 #define URL_INFO_RECORD 0x2 #define URL_INFO_RECORD_TO 0x4 #define DERP "DERP DERP DERP" struct url_info { unsigned int contents; - char author[33]; + char name[MAX_TAG_LENGTH + 1]; uint64_t record; uint64_t record_to; }; diff --git a/database/tags.c b/database/tags.c index 51d8698..d15f8b8 100644 --- a/database/tags.c +++ b/database/tags.c @@ -104,7 +104,7 @@ struct tag * tag_list(const char *tag, uint64_t offset, int *count, int directio struct tag *retlist; uint64_t n_tag_records; - if (!valid_name(tag + 1)) + if (!valid_tag_name(tag + 1)) return NULL; switch(tag[0]) { @@ -174,7 +174,7 @@ tag_list_open_failed: int tag_exists(const char *tag) { char filename[512]; - if (!valid_name(tag + 1)) + if (!valid_tag_name(tag + 1)) return 0; if (!(tag[0] == '@' || tag[0] == '#')) { diff --git a/database/util.c b/database/util.c index 6a37e49..b0ecd17 100644 --- a/database/util.c +++ b/database/util.c @@ -3,17 +3,26 @@ */ #include "database.h" +#include "config.h" -int valid_name(const char *name) { +int valid_name_len(const char *name, int maxlength) { int i; - for (i = 0; i < 32; i++) { + for (i = 0; i < maxlength; i++) { if (name[i] == 0) break; if (!VALID_CHAR(name[i])) return 0; } - if (i >= 32) + if (i >= maxlength) return 0; return 1; } + +int valid_tag_name(const char *name) { + return valid_name_len(name, MAX_TAG_LENGTH); +} + +int valid_name(const char *name) { + return valid_name_len(name, 32); +} diff --git a/database/util.h b/database/util.h index bdac3ea..4d35c93 100644 --- a/database/util.h +++ b/database/util.h @@ -4,6 +4,7 @@ #ifndef _UTIL_H #define _UTIL_H +int valid_tag_name(const char *name); int valid_name(const char *name); #endif /* _UTIL_H */ diff --git a/http/http_blerg.c b/http/http_blerg.c index 6fd8f11..025fbb0 100644 --- a/http/http_blerg.c +++ b/http/http_blerg.c @@ -264,15 +264,15 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c return respond_404(connection); ret = parse_url_info(url + 5, &info); - if ((ret & URL_INFO_AUTHOR) == 0) + if ((ret & URL_INFO_NAME) == 0) return respond_404(connection); - if (!blerg_exists(info.author)) + if (!blerg_exists(info.name)) return respond_404(connection); *ptr == NULL; - struct blerg *b = blerg_open(info.author); + struct blerg *b = blerg_open(info.name); if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) { response = create_response_for_range(b, info.record, info.record_to); @@ -317,14 +317,14 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c return respond_404(connection); ret = parse_url_info(url + 5, &info); - if ((ret & URL_INFO_AUTHOR) == 0) + if ((ret & URL_INFO_NAME) == 0) return respond_404(connection); - if (!tag_exists(info.author)) + if (!tag_exists(info.name)) return respond_404(connection); int recs = 50; - struct tag *taglist = tag_list(info.author, 0, &recs, -1); + struct tag *taglist = tag_list(info.name, 0, &recs, -1); if (recs == 0) { response = MHD_create_response_from_data(2, "[]", MHD_NO, MHD_NO); @@ -400,15 +400,15 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c return respond_404(connection); ret = parse_url_info(url + 6, &info); - if ((ret & URL_INFO_AUTHOR) == 0) + if ((ret & URL_INFO_NAME) == 0) return respond_404(connection); - if (!blerg_exists(info.author)) + if (!blerg_exists(info.name)) return respond_404(connection); *ptr == NULL; - struct blerg *b = blerg_open(info.author); + struct blerg *b = blerg_open(info.name); uint64_t record_count = blerg_get_record_count(b); blerg_close(b); -- 2.34.1