diff --git a/cmd.c b/cmd.c index c309137..0d401d8 100644 --- a/cmd.c +++ b/cmd.c @@ -86,6 +86,7 @@ cmd_setup(struct cmd *cmd, struct http_client *client) { int i; cmd->keep_alive = client->keep_alive; + cmd->w = client->w; /* keep track of the worker */ for(i = 0; i < client->header_count; ++i) { if(strcasecmp(client->headers[i].key, "If-None-Match") == 0) { diff --git a/cmd.h b/cmd.h index ea293da..c527bf6 100644 --- a/cmd.h +++ b/cmd.h @@ -43,6 +43,7 @@ struct cmd { struct http_client *pub_sub_client; redisAsyncContext *ac; + struct worker *w; }; struct subscription { diff --git a/formats/common.c b/formats/common.c index 3cd2e1a..287568a 100644 --- a/formats/common.c +++ b/formats/common.c @@ -37,7 +37,7 @@ format_send_error(struct cmd *cmd, short code, const char *msg) { struct http_response resp; if(!cmd->is_websocket && !cmd->pub_sub_client) { - http_response_init(&resp, code, msg); + http_response_init(&resp, cmd->w, code, msg); resp.http_version = cmd->http_version; http_response_set_keep_alive(&resp, cmd->keep_alive); http_response_write(&resp, cmd->fd); @@ -70,7 +70,7 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content /* start streaming */ if(cmd->started_responding == 0) { cmd->started_responding = 1; - http_response_init(&resp, 200, "OK"); + http_response_init(&resp, cmd->w, 200, "OK"); resp.http_version = cmd->http_version; if(cmd->filename) { http_response_set_header(&resp, "Content-Disposition", cmd->filename); @@ -89,9 +89,9 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content /* check If-None-Match */ if(cmd->if_none_match && strcmp(cmd->if_none_match, etag) == 0) { /* SAME! send 304. */ - http_response_init(&resp, 304, "Not Modified"); + http_response_init(&resp, cmd->w, 304, "Not Modified"); } else { - http_response_init(&resp, 200, "OK"); + http_response_init(&resp, cmd->w, 200, "OK"); if(cmd->filename) { http_response_set_header(&resp, "Content-Disposition", cmd->filename); } diff --git a/formats/custom-type.c b/formats/custom-type.c index 0267872..d38ffa5 100644 --- a/formats/custom-type.c +++ b/formats/custom-type.c @@ -41,7 +41,7 @@ custom_type_reply(redisAsyncContext *c, void *r, void *privdata) { } /* couldn't make sense of what the client wanted. */ - http_response_init(&resp, 400, "Bad Request"); + http_response_init(&resp, cmd->w, 400, "Bad Request"); http_response_set_header(&resp, "Content-Length", "0"); http_response_set_keep_alive(&resp, cmd->keep_alive); http_response_write(&resp, cmd->fd); diff --git a/http.c b/http.c index fd3081f..84ca735 100644 --- a/http.c +++ b/http.c @@ -11,13 +11,14 @@ /* HTTP Response */ void -http_response_init(struct http_response *r, int code, const char *msg) { +http_response_init(struct http_response *r, struct worker *w, int code, const char *msg) { /* remove any old data */ memset(r, 0, sizeof(struct http_response)); r->code = code; r->msg = msg; + r->w = w; http_response_set_header(r, "Server", "Webdis"); @@ -73,7 +74,13 @@ http_response_set_body(struct http_response *r, const char *body, size_t body_le r->body_len = body_len; } -int +void +http_schedule_write(const char *s, size_t sz, struct http_response *r, int keep_alive) { + + +} + +void http_response_write(struct http_response *r, int fd) { char *s = NULL, *p; @@ -135,6 +142,11 @@ http_response_write(struct http_response *r, int fd) { memcpy(s + sz, r->body, r->body_len); sz += r->body_len; } +#if 0 + if(r->w) { + http_schedule_write(s, sz, r, keep_alive); + } +#endif /* send buffer to client */ p = s; @@ -160,8 +172,6 @@ http_response_write(struct http_response *r, int fd) { free(r->headers[i].val); } free(r->headers); - - return ret == (int)sz ? 0 : 1; } static void @@ -182,7 +192,7 @@ http_crossdomain(struct http_client *c) { "\n" "\n"; - http_response_init(&resp, 200, "OK"); + http_response_init(&resp, NULL, 200, "OK"); resp.http_version = c->http_version; http_response_set_connection_header(c, &resp); http_response_set_header(&resp, "Content-Type", "application/xml"); @@ -197,7 +207,7 @@ void http_send_error(struct http_client *c, short code, const char *msg) { struct http_response resp; - http_response_init(&resp, code, msg); + http_response_init(&resp, NULL, code, msg); resp.http_version = c->http_version; http_response_set_connection_header(c, &resp); http_response_set_body(&resp, NULL, 0); @@ -223,7 +233,7 @@ void http_send_options(struct http_client *c) { struct http_response resp; - http_response_init(&resp, 200, "OK"); + http_response_init(&resp, NULL, 200, "OK"); resp.http_version = c->http_version; http_response_set_connection_header(c, &resp); diff --git a/http.h b/http.h index 45014a4..c90dc20 100644 --- a/http.h +++ b/http.h @@ -4,6 +4,7 @@ #include struct http_client; +struct worker; struct http_header { char *key; @@ -26,12 +27,14 @@ struct http_response { int chunked; int http_version; + + struct worker *w; }; /* HTTP response */ void -http_response_init(struct http_response *r, int code, const char *msg); +http_response_init(struct http_response *r, struct worker *w, int code, const char *msg); void http_response_set_header(struct http_response *r, const char *k, const char *v); @@ -39,9 +42,12 @@ http_response_set_header(struct http_response *r, const char *k, const char *v); void http_response_set_body(struct http_response *r, const char *body, size_t body_len); -int +void http_response_write(struct http_response *r, int fd); +void +http_schedule_write(const char *s, size_t sz, struct http_response *r, int keep_alive); + void http_crossdomain(struct http_client *c);