Skip to content
This repository
tree: 074ef11ff9
Fetching contributors…

Cannot retrieve contributors at this time

file 78 lines (61 sloc) 1.7 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include "common.h"
#include "cmd.h"

#include "md5/md5.h"
#include <evhttp.h>
#include <string.h>

char *etag_new(const char *p, size_t sz) {

md5_byte_t buf[16];
char *etag = calloc(34 + 1, 1);
int i;

md5_state_t pms;

md5_init(&pms);
md5_append(&pms, (const md5_byte_t *)p, (int)sz);
md5_finish(&pms, buf);

for(i = 0; i < 16; ++i) {
sprintf(etag + 1 + 2*i, "%.2x", (unsigned char)buf[i]);
}

etag[0] = '"';
etag[33] = '"';

return etag;
}

void
format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content_type) {

struct evbuffer *body;
int free_cmd = 1;

/* send reply */
body = evbuffer_new();
evbuffer_add(body, p, sz);


if(cmd_is_subscribe(cmd)) {
free_cmd = 0;

/* start streaming */
if(cmd->started_responding == 0) {
cmd->started_responding = 1;
evhttp_add_header(cmd->rq->output_headers, "Content-Type",
cmd->mime?cmd->mime:content_type);
evhttp_send_reply_start(cmd->rq, 200, "OK");
}
evhttp_send_reply_chunk(cmd->rq, body);

} else {
/* compute ETag */
char *etag = etag_new(p, sz);
const char *if_none_match;

/* check If-None-Match */
if((if_none_match = evhttp_find_header(cmd->rq->input_headers, "If-None-Match"))
&& strcmp(if_none_match, etag) == 0) {

/* SAME! send 304. */
evhttp_send_reply(cmd->rq, 304, "Not Modified", NULL);
} else {
evhttp_add_header(cmd->rq->output_headers, "Content-Type",
cmd->mime?cmd->mime:content_type);
evhttp_add_header(cmd->rq->output_headers, "ETag", etag);
evhttp_send_reply(cmd->rq, 200, "OK", body);
}
free(etag);
}
/* cleanup */
evbuffer_free(body);
if(free_cmd) {
evhttp_clear_headers(&cmd->uri_params);
cmd_free(cmd);
}
}
Something went wrong with that request. Please try again.