Skip to content
Browse files

HTTP version in reply.

  • Loading branch information...
1 parent b19fbc7 commit 239c9004dcd34ccb747c4c0bc72ac41f9fb4a068 @nicolasff committed Apr 8, 2011
Showing with 30 additions and 12 deletions.
  1. +1 −0 client.c
  2. +4 −3 client.h
  3. +4 −1 cmd.c
  4. +8 −4 cmd.h
  5. +2 −0 formats/common.c
  6. +4 −1 http.c
  7. +2 −1 http.h
  8. +5 −2 websocket.c
View
1 client.c
@@ -146,6 +146,7 @@ http_client_on_message_complete(struct http_parser *p) {
if(c->parser.http_major == 1 && c->parser.http_minor == 1) { /* 1.1 */
c->keep_alive = 1;
}
+ c->http_version = c->parser.http_minor;
if(p->upgrade) { /* WebSocket, don't execute just yet */
c->is_websocket = 1;
View
7 client.h
@@ -31,9 +31,10 @@ struct http_client {
last_cb_t last_cb;
/* various flags. TODO: bit map */
- int keep_alive;
- int broken;
- int is_websocket;
+ int keep_alive:1;
+ int broken:1;
+ int is_websocket:1;
+ int http_version:1;
/* HTTP data */
char *path;
View
5 cmd.c
@@ -81,7 +81,7 @@ decode_uri(const char *uri, size_t length, size_t *out_len, int always_decode_pl
}
/* setup headers */
-static void
+void
cmd_setup(struct cmd *cmd, struct http_client *client) {
int i;
@@ -108,6 +108,9 @@ cmd_setup(struct cmd *cmd, struct http_client *client) {
cmd->jsonp = client->jsonp;
client->jsonp = NULL;
}
+
+ cmd->fd = client->fd;
+ cmd->http_version = client->http_version;
}
View
12 cmd.h
@@ -24,14 +24,15 @@ struct cmd {
/* HTTP data */
char *mime; /* forced output content-type */
- int mime_free;
char *if_none_match; /* used with ETags */
char *jsonp; /* jsonp wrapper */
- int keep_alive;
+ int keep_alive:1;
+ int mime_free:1; /* need to free mime buffer */
/* various flags */
- int started_responding;
- int is_websocket;
+ int started_responding:1;
+ int is_websocket:1;
+ int http_version:1;
};
struct subscription {
@@ -60,4 +61,7 @@ cmd_is_subscribe(struct cmd *cmd);
void
cmd_send(redisAsyncContext *ac, formatting_fun f_format, struct cmd *cmd);
+void
+cmd_setup(struct cmd *cmd, struct http_client *client);
+
#endif
View
2 formats/common.c
@@ -51,6 +51,7 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content
const char *ct = cmd->mime?cmd->mime:content_type;
cmd->started_responding = 1;
http_response_init(&resp, 200, "OK");
+ resp.http_version = cmd->http_version;
http_response_set_header(&resp, "Content-Type", ct);
http_response_set_header(&resp, "Connection", "Keep-Alive");
http_response_set_header(&resp, "Transfer-Encoding", "Chunked");
@@ -73,6 +74,7 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content
http_response_set_header(&resp, "ETag", etag);
http_response_set_body(&resp, p, sz);
}
+ resp.http_version = cmd->http_version;
if(cmd->keep_alive) {
http_response_set_header(&resp, "Connection", "Keep-Alive");
} else {
View
5 http.c
@@ -79,7 +79,7 @@ http_response_write(struct http_response *r, int fd) {
sz = sizeof("HTTP/1.x xxx ")-1 + strlen(r->msg) + 2;
s = calloc(sz + 1, 1);
- ret = sprintf(s, "HTTP/1.1 %d %s\r\n", r->code, r->msg);
+ ret = sprintf(s, "HTTP/1.%d %d %s\r\n", (r->http_version?1:0), r->code, r->msg);
p = s;
if(r->code == 200 && r->body) {
@@ -165,6 +165,7 @@ http_crossdomain(struct http_client *c) {
"</cross-domain-policy>\n";
http_response_init(&resp, 200, "OK");
+ resp.http_version = c->http_version;
http_response_set_connection_header(c, &resp);
http_response_set_header(&resp, "Content-Type", "application/xml");
http_response_set_body(&resp, out, sizeof(out)-1);
@@ -179,6 +180,7 @@ http_send_error(struct http_client *c, short code, const char *msg) {
struct http_response resp;
http_response_init(&resp, code, msg);
+ resp.http_version = c->http_version;
http_response_set_connection_header(c, &resp);
http_response_set_body(&resp, NULL, 0);
@@ -201,6 +203,7 @@ http_send_options(struct http_client *c) {
struct http_response resp;
http_response_init(&resp, 200, "OK");
+ resp.http_version = c->http_version;
http_response_set_connection_header(c, &resp);
http_response_set_header(&resp, "Content-Type", "text/html");
View
3 http.h
@@ -24,7 +24,8 @@ struct http_response {
const char *body;
size_t body_len;
- int chunked;
+ int chunked:1;
+ int http_version:1;
};
/* HTTP response */
View
7 websocket.c
@@ -154,11 +154,14 @@ ws_execute(struct http_client *c, const char *frame, size_t frame_len) {
if(fun_extract) {
struct cmd *cmd = fun_extract(c, frame, frame_len);
if(cmd) {
+ /* copy client info into cmd. */
+ cmd_setup(cmd, c);
cmd->is_websocket = 1;
- cmd->fd = c->fd;
- /* TODO: clean this mess */
+ /* get redis connection from pool */
redisAsyncContext *ac = (redisAsyncContext*)pool_get_context(c->w->pool);
+
+ /* send it off */
cmd_send(ac, json_reply, cmd);
return 0;
}

0 comments on commit 239c900

Please sign in to comment.
Something went wrong with that request. Please try again.