Skip to content

Commit

Permalink
HTTP version in reply.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Apr 8, 2011
1 parent b19fbc7 commit 239c900
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions client.c
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions client.h
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion cmd.c
Expand Up @@ -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;
Expand All @@ -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;
}


Expand Down
12 changes: 8 additions & 4 deletions cmd.h
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions formats/common.c
Expand Up @@ -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");
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion http.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion http.h
Expand Up @@ -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 */
Expand Down
7 changes: 5 additions & 2 deletions websocket.c
Expand Up @@ -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;
}
Expand Down

0 comments on commit 239c900

Please sign in to comment.