Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bugfix, restored forced content-type.

  • Loading branch information...
commit bbec5c65931a08dc08431a41945e018824d755dd 1 parent 2e72167
@nicolasff authored
Showing with 77 additions and 30 deletions.
  1. +9 −17 cmd.c
  2. +2 −1  cmd.h
  3. +5 −3 formats/common.c
  4. +53 −8 http.c
  5. +8 −1 http.h
View
26 cmd.c
@@ -2,6 +2,7 @@
#include "server.h"
#include "conf.h"
#include "acl.h"
+#include "http.h"
#include "formats/json.h"
#include "formats/raw.h"
@@ -114,11 +115,8 @@ cmd_run(struct server *s, struct http_client *client,
cmd = cmd_new(client, param_count);
- /* FIXME: parse URI parameters */
- /* evhttp_parse_query(uri, &cmd->uri_params); */
-
/* get output formatting function */
- uri_len = cmd_select_format(cmd, uri, uri_len, &f_format);
+ uri_len = cmd_select_format(client, cmd, uri, uri_len, &f_format);
/* check if we only have one command or more. */
slash = memchr(uri, '/', uri_len);
@@ -197,9 +195,9 @@ cmd_run(struct server *s, struct http_client *client,
* Select Content-Type and processing function.
*/
int
-cmd_select_format(struct cmd *cmd, const char *uri, size_t uri_len, formatting_fun *f_format) {
+cmd_select_format(struct http_client *client, struct cmd *cmd,
+ const char *uri, size_t uri_len, formatting_fun *f_format) {
- struct evkeyval *kv;
const char *ext;
int ext_len = -1;
unsigned int i;
@@ -252,18 +250,12 @@ cmd_select_format(struct cmd *cmd, const char *uri, size_t uri_len, formatting_f
}
/* FIXME:the user can force it with ?type=some/thing */
- /*
- TAILQ_FOREACH(kv, &cmd->uri_params, next) {
- if(strcmp(kv->key, "type") == 0) {
-
- *f_format = custom_type_reply;
- cmd->mime = strdup(kv->value);
- cmd->mime_free = 1;
-
- break;
- }
+ if(client->qs_type.s) {
+ *f_format = custom_type_reply;
+ cmd->mime = strdup(client->qs_type.s);
+ cmd->mime_free = 1;
}
- */
+
return uri_len - ext_len - 1;
}
View
3  cmd.h
@@ -48,7 +48,8 @@ cmd_run(struct server *s, struct http_client *client,
const char *body, size_t body_len);
int
-cmd_select_format(struct cmd *cmd, const char *uri, size_t uri_len, formatting_fun *f_format);
+cmd_select_format(struct http_client *client, struct cmd *cmd,
+ const char *uri, size_t uri_len, formatting_fun *f_format);
int
cmd_is_subscribe(struct cmd *cmd);
View
8 formats/common.c
@@ -39,8 +39,9 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content
/* start streaming */
if(cmd->started_responding == 0) {
+ const char *ct = cmd->mime?cmd->mime:content_type;
cmd->started_responding = 1;
- http_set_header(&cmd->client->out_content_type, cmd->mime?cmd->mime:content_type);
+ http_set_header(&cmd->client->out_content_type, ct, strlen(ct));
/*FIXME:
evhttp_send_reply_start(cmd->rq, 200, "OK");
*/
@@ -58,8 +59,9 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content
/* SAME! send 304. */
http_send_reply(cmd->client, 304, "Not Modified", NULL, 0);
} else {
- http_set_header(&cmd->client->out_content_type, cmd->mime?cmd->mime:content_type);
- http_set_header(&cmd->client->out_etag, etag);
+ const char *ct = cmd->mime?cmd->mime:content_type;
+ http_set_header(&cmd->client->out_content_type, ct, strlen(ct));
+ http_set_header(&cmd->client->out_etag, etag, strlen(etag));
http_send_reply(cmd->client, 200, "OK", p, sz);
}
#endif
View
61 http.c
@@ -19,6 +19,7 @@ http_client_new(int fd, struct server *s) {
c->settings.on_message_complete = http_on_complete;
c->settings.on_header_field = http_on_header_name;
c->settings.on_header_value = http_on_header_value;
+ c->settings.on_query_string = http_on_query_string;
http_parser_init(&c->parser, HTTP_REQUEST);
c->parser.data = c;
@@ -82,6 +83,12 @@ http_client_cleanup(struct http_client *c) {
free(c->out_etag.s);
memset(&c->out_etag, 0, sizeof(str_t));
+ free(c->qs_type.s);
+ memset(&c->qs_type, 0, sizeof(str_t));
+
+ free(c->qs_jsonp.s);
+ memset(&c->qs_jsonp, 0, sizeof(str_t));
+
free(c->buffer);
c->buffer = NULL;
c->sz = 0;
@@ -185,7 +192,7 @@ http_crossdomain(struct http_client *c) {
http_response_init(&resp, 200, "OK");
http_response_set_header(&resp, "Content-Type", "application/xml");
- sprintf(content_length, "%zd", sizeof(content_length));
+ sprintf(content_length, "%zd", sizeof(out)-1);
http_response_set_header(&resp, "Content-Length", content_length);
http_response_set_body(&resp, out, sizeof(out)-1);
@@ -300,9 +307,8 @@ http_send_reply(struct http_client *c, short code, const char *msg,
}
void
-http_set_header(str_t *h, const char *p) {
+http_set_header(str_t *h, const char *p, size_t sz) {
- size_t sz = strlen(p);
h->s = calloc(1, 1+sz);
memcpy(h->s, p, sz);
h->sz = sz;
@@ -345,6 +351,50 @@ http_on_header_value(http_parser *p, const char *at, size_t length) {
c->last_header_name.s = NULL;
return 0;
}
+/**
+ * Called when the query string is found
+ */
+int
+http_on_query_string(http_parser *parser, const char *at, size_t length) {
+
+ struct http_client *c = parser->data;
+ const char *p = at;
+
+ while(p < at + length) {
+
+ const char *key = p, *val;
+ int key_len, val_len;
+ char *eq = memchr(key, '=', length - (p-at));
+ if(!eq || eq > at + length) { /* last argument */
+ break;
+ } else { /* found an '=' */
+ char *and;
+ val = eq + 1;
+ key_len = eq - key;
+ p = eq + 1;
+
+ and = memchr(p, '&', length - (p-at));
+ if(!and || and > at + length) {
+ val_len = at + length - p; /* last arg */
+ } else {
+ val_len = and - val; /* cur arg */
+ p = and + 1;
+ }
+
+ if(key_len == 4 && strncmp(key, "type", 4) == 0) {
+ http_set_header(&c->qs_type, val, val_len);
+ } else if(key_len == 5 && strncmp(key, "jsonp", 5) == 0) {
+ http_set_header(&c->qs_jsonp, val, val_len);
+ }
+
+ if(!and) {
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
/* HTTP Response */
void
@@ -413,11 +463,6 @@ http_response_send(struct http_response *r, int fd) {
memcpy(s + sz, r->body, r->body_len);
sz += r->body_len;
}
- /*
- printf("sz=%zd, s=["); fflush(stdout);
- write(1, s, sz);
- printf("]\n");
- */
ret = write(fd, s, sz);
free(s);
View
9 http.h
@@ -36,6 +36,9 @@ struct http_client {
str_t out_content_type;
str_t out_etag;
+ str_t qs_type;
+ str_t qs_jsonp;
+
/* private, used in HTTP parser */
str_t last_header_name;
};
@@ -78,8 +81,12 @@ http_on_header_value(http_parser*, const char *at, size_t length);
int
http_on_complete(http_parser*);
+int
+http_on_query_string(http_parser*, const char *at, size_t length);
+
+
void
-http_set_header(str_t *h, const char *p);
+http_set_header(str_t *h, const char *p, size_t sz);
void
http_send_reply(struct http_client *c, short code, const char *msg,
Please sign in to comment.
Something went wrong with that request. Please try again.