Permalink
Browse files

Merge commit 'c7fa'

  • Loading branch information...
2 parents 9f52212 + c7fa58b commit 51f5af9ddb8745aed17aaa52045ea51e37a1c90e @thejh committed Jun 2, 2012
Showing with 21 additions and 17 deletions.
  1. +13 −12 src/main.c
  2. +6 −3 src/outstream.c
  3. +2 −2 src/outstream.h
View
25 src/main.c
@@ -359,10 +359,9 @@ int on_server_headers_complete(http_parser *p) {
assert(b-buf == len);
free_headers(&a->response_headers);
- outstream_send(&a->client->outstream, buf, len);
-
free(status_str);
- return 0;
+
+ return outstream_send(&a->client->outstream, buf, len);
}
// XXX HOT CALLBACK! XXX
@@ -381,9 +380,7 @@ int on_server_body(http_parser *p, const char *data, size_t size) {
assert(size != 0);
chunkify(&d, &size, 0);
- outstream_send(&a->client->outstream, d, size);
-
- return 0;
+ return outstream_send(&a->client->outstream, d, size);
}
int on_server_message_complete(http_parser *p) {
@@ -393,7 +390,9 @@ int on_server_message_complete(http_parser *p) {
char *data = malloc(strlen(final_chunk));
memcpy(data, final_chunk, strlen(final_chunk));
- outstream_send(&a->client->outstream, data, strlen(final_chunk));
+ if (outstream_send(&a->client->outstream, data, strlen(final_chunk))) {
+ return 1;
+ }
// disassociate
printd("unbinding agent\n");
@@ -507,10 +506,12 @@ int on_client_headers_complete(http_parser *p) {
hostname[hostname_end - w->url - 7] = '\0';
if (bl_check(hostname) == 1) {
- outstream_send(&w->outstream, strdup(DENY_RESPONSE), strlen(DENY_RESPONSE));
#ifdef PRINT_URLS
printbuf("\033[2;31m%s\033[0m\n", w->url, w->url_size);
#endif
+ if (outstream_send(&w->outstream, strdup(DENY_RESPONSE), strlen(DENY_RESPONSE))) {
+ return 1;
+ }
w->parser_dontpause = 1;
return 0;
}
@@ -519,10 +520,12 @@ int on_client_headers_complete(http_parser *p) {
struct http_agent *agent = get_agent(hostname, w);
if (agent == NULL) {
// invalid hostname or so
- outstream_send(&w->outstream, strdup(INVAL_RESPONSE), strlen(INVAL_RESPONSE));
#ifdef PRINT_URLS
printbuf("\033[2;33m%s\033[0m\n", w->url, w->url_size);
#endif
+ if (outstream_send(&w->outstream, strdup(INVAL_RESPONSE), strlen(INVAL_RESPONSE))) {
+ return 1;
+ }
w->parser_dontpause = 1;
return 0;
}
@@ -583,11 +586,9 @@ int on_client_headers_complete(http_parser *p) {
*(pos++) = '\0';
assert(pos - header_buffer == header_buffer_size);
printd("HEADER READY FOR SENDING: \n<<<<<<<<<<<<<<<<<<<<\n%s\n>>>>>>>>>>>>>>>>>>>>\n", header_buffer);
- outstream_send(&agent->outstream, header_buffer, header_buffer_size - 1 /* the nullbyte is for debugging only, stupid! */);
-
free_headers(&w->request_headers);
- return 0;
+ return outstream_send(&agent->outstream, header_buffer, header_buffer_size - 1 /* the nullbyte is for debugging only, stupid! */);
}
int on_client_body(http_parser *p, const char *data, size_t length) {
View
9 src/outstream.c
@@ -75,14 +75,15 @@ void outstream_nuke(struct outstream *s) {
ev_io_stop(ev_default_loop(0), &s->watcher);
}
-void outstream_send(struct outstream *s, char *buf, size_t len) {
+// 0 means success, 1 means failure
+int outstream_send(struct outstream *s, char *buf, size_t len) {
char *free_ptr = buf;
if (s->first_buf == NULL) {
ssize_t written = write(s->watcher.fd, buf, len);
if (written == -1 && errno != EAGAIN) {
- s->error_cb(s); return;
+ s->error_cb(s); return 1;
} else if (written == 0) {
- s->error_cb(s); return;
+ s->error_cb(s); return 1;
}
if (written == len) { free(buf); return; } // yaaay, no buffer magic needed!
@@ -108,4 +109,6 @@ void outstream_send(struct outstream *s, char *buf, size_t len) {
if (s->pressure >= OUTSTREAM_HIGH && s->pressure - len < OUTSTREAM_HIGH && s->input_watcher != NULL) {
alter_ev_io_events(s->input_watcher, 0, EV_READ);
}
+
+ return 0;
}
View
4 src/outstream.h
@@ -31,8 +31,8 @@ void outstream_init(struct outstream *s, int fd, void (*error_cb)(struct outstre
// Use this to add/remove backpressure.
void outstream_input_set(struct outstream *s, struct ev_io *input_watcher);
-// Takes responsibility for free()'ing buf.
-void outstream_send(struct outstream *s, char *buf, size_t len);
+// Takes responsibility for free()'ing buf. 0 means success, 1 means failure.
+int outstream_send(struct outstream *s, char *buf, size_t len);
void outstream_nuke(struct outstream *s);

0 comments on commit 51f5af9

Please sign in to comment.