You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB
C

14 years ago
#include "common.h"
#include "cmd.h"
#include "http.h"
14 years ago
#include "md5/md5.h"
#include <string.h>
/* TODO: replace this with a faster hash function */
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;
}
14 years ago
void
format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content_type) {
int free_cmd = 1;
if(cmd_is_subscribe(cmd)) {
free_cmd = 0;
/* start streaming */
if(cmd->started_responding == 0) {
cmd->started_responding = 1;
http_set_header(&cmd->client->out_content_type, cmd->mime?cmd->mime:content_type);
/*FIXME:
14 years ago
evhttp_send_reply_start(cmd->rq, 200, "OK");
*/
14 years ago
}
/*FIXME: evhttp_send_reply_chunk(cmd->rq, body); */
14 years ago
} else {
/* compute ETag */
char *etag = etag_new(p, sz);
14 years ago
const char *if_none_match = cmd->client->header_if_none_match.s;
/* FIXME */
#if 1
/* check If-None-Match */
14 years ago
if(if_none_match && strncmp(if_none_match, etag, cmd->client->header_if_none_match.sz) == 0) {
/* SAME! send 304. */
14 years ago
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);
http_send_reply(cmd->client, 200, "OK", p, sz);
}
#endif
free(etag);
14 years ago
}
/* cleanup */
if(free_cmd) {
/*FIXME: evhttp_clear_headers(&cmd->uri_params); */
14 years ago
cmd_free(cmd);
}
}