Permalink
Browse files

initial support for ?format=txt and provide some common request metho…

…ds like get_int_argument()
  • Loading branch information...
jehiah committed Mar 23, 2011
1 parent 92e294f commit 3f897765d6f95e2e443c70bf7fa9dfa576a163ad
View
@@ -68,18 +68,6 @@ can_kick(struct cli *client) {
return 0;
}
-void
-argtoi(struct evkeyvalq *args, char *key, int *val, int def)
-{
- char *tmp;
-
- *val = def;
- tmp = (char *)evhttp_find_header(args, (const char *)key);
- if (tmp) {
- *val = atoi(tmp);
- }
-}
-
void
clients_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
@@ -234,7 +222,7 @@ void sub_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
totalConns++;
evhttp_parse_query(req->uri, &args);
client = calloc(1, sizeof(*client));
- argtoi(&args, "multipart", &client->multipart, 1);
+ client->multipart = get_int_argument(&args, "multipart", 1);
client->req = req;
client->connection_id = totalConns;
client->connect_time = time(NULL);
@@ -15,7 +15,6 @@ void finalize_json(struct evhttp_request *req, struct evbuffer *evb,
struct evkeyvalq *args, struct json_object *jsobj);
int open_db(char *addr, int port, TCRDB **rdb);
void db_reconnect(int fd, short what, void *ctx);
-void argtoi(struct evkeyvalq *args, char *key, int *val, int def);
void db_error_to_json(int code, struct json_object *jsobj);
void idx_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
void del_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
@@ -87,17 +86,6 @@ void db_reconnect(int fd, short what, void *ctx)
evtimer_add(&ev, &tv);
}
-void argtoi(struct evkeyvalq *args, char *key, int *val, int def)
-{
- char *tmp;
-
- *val = def;
- tmp = (char *)evhttp_find_header(args, (const char *)key);
- if (tmp) {
- *val = atoi(tmp);
- }
-}
-
void db_error_to_json(int code, struct json_object *jsobj)
{
fprintf(stderr, "error(%d): %s\n", code, tcrdberrmsg(code));
@@ -122,9 +110,9 @@ void idx_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
evhttp_parse_query(req->uri, &args);
key = (char *)evhttp_find_header(&args, "key");
- argtoi(&args, "max", &max, 1000);
- argtoi(&args, "length", &len, 10);
- argtoi(&args, "offset", &off, 0);
+ max = get_int_argument(&args, "max", 1000);
+ len = get_int_argument(&args, "length", 10);
+ off = get_int_argument(&args, "offset", 0);
if (key == NULL) {
evhttp_send_error(req, 400, "key is required");
evhttp_clear_headers(&args);
@@ -217,8 +205,7 @@ void put_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
}
jsonPtr = json_tokener_parse(kvs);
-
- if (jsonPtr == 0xfffffffffffffffc) {
+ if (!jsonPtr) {
evhttp_send_error(req, 400, "kvs json is invalid");
evhttp_clear_headers(&args);
return;
View
@@ -18,8 +18,6 @@ void finalize_json(struct evhttp_request *req, struct evbuffer *evb, struct evke
int open_db(char *addr, int port, TCRDB **rdb);
void db_reconnect(int fd, short what, void *ctx);
void db_error_to_json(int code, struct json_object *jsobj);
-void argtoi(struct evkeyvalq *args, char *key, int *val, int def);
-void argtof(struct evkeyvalq *args, char *key, double *val, double def);
void del_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
void put_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
void get_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
@@ -141,28 +139,6 @@ void db_reconnect(int fd, short what, void *ctx)
evtimer_add(&ev, &tv);
}
-void argtoi(struct evkeyvalq *args, char *key, int *val, int def)
-{
- char *tmp;
-
- *val = def;
- tmp = (char *)evhttp_find_header(args, (const char *)key);
- if (tmp) {
- *val = atoi(tmp);
- }
-}
-
-void argtof(struct evkeyvalq *args, char *key, double *val, double def)
-{
- char *tmp;
-
- *val = def;
- tmp = (char *)evhttp_find_header(args, (const char *)key);
- if (tmp) {
- *val = atof(tmp);
- }
-}
-
void db_error_to_json(int code, struct json_object *jsobj)
{
fprintf(stderr, "error(%d): %s\n", code, tcrdberrmsg(code));
@@ -181,9 +157,9 @@ void box_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
evhttp_parse_query(req->uri, &args);
- argtof(&args, "lat", &lat, 0);
- argtof(&args, "lng", &lng, 0);
- argtof(&args, "miles", &miles, 0);
+ lat = get_double_argument(&args, "lat", 0);
+ lng = get_double_argument(&args, "lng", 0);
+ miles = get_double_argument(&args, "miles", 0);
geo_box(lat, lng, miles, &ulat, &ulng, &llat, &llng);
@@ -205,10 +181,10 @@ void distance_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
evhttp_parse_query(req->uri, &args);
- argtof(&args, "lat1", &lat1, 0);
- argtof(&args, "lng1", &lng1, 0);
- argtof(&args, "lat2", &lat2, 0);
- argtof(&args, "lng2", &lng2, 0);
+ lat1 = get_double_argument(&args, "lat1", 0);
+ lat1 = get_double_argument(&args, "lng1", 0);
+ lat2 = get_double_argument(&args, "lat2", 0);
+ lat2 = get_double_argument(&args, "lng2", 0);
jsobj = json_object_new_object();
json_object_object_add(jsobj, "distance", json_object_new_double(geo_distance(lat1, lng1, lat2, lng2)));
@@ -320,8 +296,8 @@ void put_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
}
evhttp_parse_query(req->uri, &args);
- argtof(&args, "lat", &lat, 0);
- argtof(&args, "lng", &lng, 0);
+ lat = get_double_argument(&args, "lat", 0);
+ lng = get_double_argument(&args, "lng", 0);
id = (char *)evhttp_find_header(&args, "id");
data = (char *)evhttp_find_header(&args, "data");
@@ -385,10 +361,10 @@ void search_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
evhttp_parse_query(req->uri, &args);
- argtof(&args, "lat", &lat, 0);
- argtof(&args, "lng", &lng, 0);
- argtof(&args, "miles", &miles, 0);
- argtoi(&args, "max", &max, 1);
+ lat = get_int_argument(&args, "lat", 0);
+ lng = get_int_argument(&args, "lng", 0);
+ miles = get_int_argument(&args, "miles", 0);
+ max = get_int_argument(&args, "max", 1);
geo_box(lat, lng, miles, &minlat, &minlng, &maxlat, &maxlng);
View
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
+#include <string.h>
#include "queue.h"
#include "simplehttp.h"
#include "request.h"
@@ -98,3 +99,37 @@ void simplehttp_async_finish(struct evhttp_request *req)
simplehttp_request_finish(req, entry);
}
}
+
+
+int get_argument_format(struct evkeyvalq *args)
+{
+ int format_code = json_format;
+ char *format = (char *)evhttp_find_header(args, "format");
+ if (format && !strncmp(format, "txt", 3)) {
+ format_code = txt_format;
+ }
+ return format_code;
+}
+
+int get_int_argument(struct evkeyvalq *args, char *key, int default_value)
+{
+ char *tmp;
+ if (!key) return default_value;
+ tmp = (char *)evhttp_find_header(args, (const char *)key);
+ if (tmp) {
+ return atoi(tmp);
+ }
+ return default_value;
+}
+
+double get_double_argument(struct evkeyvalq *args, char *key, double default_value)
+{
+ char *tmp;
+ if (!key) return default_value;
+ tmp = (char *)evhttp_find_header(args, (const char *)key);
+ if (tmp) {
+ return atof(tmp);
+ }
+ return default_value;
+}
+
View
@@ -58,5 +58,10 @@ void free_async_callback_group(struct AsyncCallbackGroup *callback_group);
void init_async_connection_pool(int enable_request_logging);
void free_async_connection_pool();
+enum response_formats {json_format, txt_format};
+int get_argument_format(struct evkeyvalq *args);
+int get_int_argument(struct evkeyvalq *args, char *key, int default_value);
+double get_double_argument(struct evkeyvalq *args, char *key, double default_value);
+
#endif
Oops, something went wrong.

0 comments on commit 3f89776

Please sign in to comment.