From dac5eccde7f3da52db391f1e4144f6d48b73d30d Mon Sep 17 00:00:00 2001 From: Nicolas Favre-Felix Date: Sat, 22 Jan 2011 16:39:48 +0100 Subject: [PATCH] Fix ETag. --- formats/common.c | 9 +++------ formats/custom-type.c | 5 +++-- formats/raw.c | 5 ++--- http.c | 17 ++++++++++++----- http.h | 1 + 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/formats/common.c b/formats/common.c index 13200b8..4b08453 100644 --- a/formats/common.c +++ b/formats/common.c @@ -36,7 +36,6 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content if(cmd_is_subscribe(cmd)) { free_cmd = 0; - printf("wtf\n"); /* start streaming */ if(cmd->started_responding == 0) { @@ -51,15 +50,13 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content } else { /* compute ETag */ char *etag = etag_new(p, sz); - const char *if_none_match; + const char *if_none_match = cmd->client->header_if_none_match.s; /* FIXME */ #if 1 /* check If-None-Match */ - if(0 /*FIXME:(if_none_match = evhttp_find_header(cmd->rq->input_headers, "If-None-Match")) - && strcmp(if_none_match, etag) == 0*/) { - + if(if_none_match && strncmp(if_none_match, etag, cmd->client->header_if_none_match.sz) == 0) { /* SAME! send 304. */ - /* evhttp_send_reply(cmd->rq, 304, "Not Modified", NULL); */ + 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); diff --git a/formats/custom-type.c b/formats/custom-type.c index 63461f0..605ba4e 100644 --- a/formats/custom-type.c +++ b/formats/custom-type.c @@ -1,6 +1,7 @@ #include "custom-type.h" #include "cmd.h" #include "common.h" +#include "http.h" #include #include @@ -16,7 +17,7 @@ custom_type_reply(redisAsyncContext *c, void *r, void *privdata) { int int_len; if(reply == NULL) { - /* FIXME: evhttp_send_reply(cmd->rq, 404, "Not Found", NULL); */ + http_send_reply(cmd->client, 404, "Not Found", NULL, 0); return; } @@ -39,7 +40,7 @@ custom_type_reply(redisAsyncContext *c, void *r, void *privdata) { } /* couldn't make sense of what the client wanted. */ - /* FIXME: evhttp_send_reply(cmd->rq, 400, "Bad request", NULL); */ + http_send_reply(cmd->client, 400, "Bad request", NULL, 0); cmd_free(cmd); } diff --git a/formats/raw.c b/formats/raw.c index cd6ff6a..1850c0b 100644 --- a/formats/raw.c +++ b/formats/raw.c @@ -1,6 +1,7 @@ #include "raw.h" #include "cmd.h" #include "common.h" +#include "http.h" #include #include @@ -19,9 +20,7 @@ raw_reply(redisAsyncContext *c, void *r, void *privdata) { (void)c; if (reply == NULL) { - /* FIXME - evhttp_send_reply(cmd->rq, 404, "Not Found", NULL); - */ + http_send_reply(cmd->client, 404, "Not Found", NULL, 0); return; } diff --git a/http.c b/http.c index 5e5ab8c..435af05 100644 --- a/http.c +++ b/http.c @@ -96,12 +96,14 @@ http_client_reset(struct http_client *c) { return; } - memset(&c->path, 0, sizeof(c->path)); - memset(&c->body, 0, sizeof(c->body)); + memset(&c->path, 0, sizeof(str_t)); + memset(&c->body, 0, sizeof(str_t)); + memset(&c->header_connection, 0, sizeof(str_t)); + memset(&c->header_if_none_match, 0, sizeof(str_t)); free((char*)c->out_content_type.s); - memset(&c->out_content_type, 0, sizeof(c->out_content_type)); - memset(&c->out_etag, 0, sizeof(c->out_etag)); + memset(&c->out_content_type, 0, sizeof(str_t)); + memset(&c->out_etag, 0, sizeof(str_t)); free(c->buffer); c->buffer = NULL; @@ -199,9 +201,11 @@ http_send_reply(struct http_client *c, short code, const char *msg, ret = snprintf(out, sz, "HTTP/1.1 %d %s\r\n" "Content-Type: %s\r\n" "Content-Length: %zd\r\n" + "ETag: %s\r\n" "Connection: %s\r\n" "Server: Webdis\r\n" "\r\n", code, msg, ct, body_len, + (c->out_etag.s ? c->out_etag.s : "\"\""), (http_client_keep_alive(c) ? "Keep-Alive" : "Close") ); @@ -252,9 +256,12 @@ http_on_header_value(http_parser *p, const char *at, size_t length) { struct http_client *c = p->data; - if(strncmp("Connection", c->last_header_name.s, length) == 0) { + if(strncmp("Connection", c->last_header_name.s, c->last_header_name.sz) == 0) { c->header_connection.s = at; c->header_connection.sz = length; + } else if(strncmp("If-None-Match", c->last_header_name.s, c->last_header_name.sz) == 0) { + c->header_if_none_match.s = at; + c->header_if_none_match.sz = length; } return 0; } diff --git a/http.h b/http.h index 574cae8..7806040 100644 --- a/http.h +++ b/http.h @@ -31,6 +31,7 @@ struct http_client { str_t path; str_t body; str_t header_connection; + str_t header_if_none_match; str_t out_content_type; str_t out_etag;