0
@@ -145,6 +145,7 @@ void content_length_cb(void *data, const char *at, size_t length)
0
+const char* localhost_str = "0.0.0.0";
0
void dispatch(ebb_client *client)
0
ebb_server *server = client->server;
0
@@ -153,14 +154,16 @@ void dispatch(ebb_client *client)
0
/* Set the env variables */
0
- // env_add_const(client, EBB_SERVER_NAME
0
- // , strlen(server->address)
0
- // env_add_const(client, EBB_SERVER_PORT
0
- // , strlen(server->port)
0
+ env_add_const(client, EBB_SERVER_NAME
0
+ env_add_const(client, EBB_SERVER_PORT
0
+ , strlen(server->port)
0
server->request_cb(client, server->request_cb_data);
0
@@ -179,78 +182,6 @@ void on_timeout(struct ev_loop *loop, ev_timer *watcher, int revents)
0
-void* read_body_into_file(void *_client)
0
- ebb_client *client = (ebb_client*)_client;
0
- static unsigned int id;
0
- assert(client->server->open);
0
- assert(client->content_length > 0);
0
- assert(http_parser_is_finished(&client->parser));
0
- /* set blocking socket */
0
- int flags = fcntl(client->fd, F_GETFL, 0);
0
- assert(0 <= fcntl(client->fd, F_SETFL, flags & ~O_NONBLOCK));
0
- sprintf(client->upload_file_filename, "/tmp/ebb_upload_%010d", id++);
0
- tmpfile = fopen(client->upload_file_filename, "w+");
0
- if(tmpfile == NULL) ebb_error("Cannot open tmpfile %s", client->upload_file_filename);
0
- client->upload_file = tmpfile;
0
- size_t body_head_length = client->read - client->parser.nread;
0
- size_t written = 0, r;
0
- while(written < body_head_length) {
0
- r = fwrite( client->read_buffer + sizeof(char)*(client->parser.nread + written)
0
- , body_head_length - written
0
- ebb_client_close(client);
0
- // ebb_debug("wrote request header to file. written: %d, content_length: %d", written, client->content_length);
0
- while(written < client->content_length) {
0
- received = recv(client->fd
0
- , min(client->content_length - written, bufsize)
0
- if(received < 0) goto error;
0
- client->read += received;
0
- rv = fwrite( buffer + w*sizeof(char)
0
- if(rv <= 0) goto error;
0
- // ebb_debug("%d bytes written to file %s", written, client->upload_file_filename);
0
- ebb_client_close(client);
0
-#define total_request_size (client->content_length + client->parser.nread)
0
void on_readable( struct ev_loop *loop
0
@@ -262,9 +193,10 @@ void on_readable( struct ev_loop *loop
0
assert(client->server->open);
0
assert(client->server->loop == loop);
0
assert(&client->read_watcher == watcher);
0
+ assert(FALSE == http_parser_is_finished(&client->parser));
0
ssize_t read = recv( client->fd
0
- , client->read_buffer + client->read
0
+ , client->request_buffer + client->read
0
, EBB_BUFFERSIZE - client->read - 1
0
@@ -272,32 +204,19 @@ void on_readable( struct ev_loop *loop
0
ev_timer_again(loop, &client->timeout_watcher);
0
- if(FALSE == http_parser_is_finished(&client->parser)) {
0
- client->read_buffer[client->read] = '\0'; /* make ragel happy */
0
- http_parser_execute( &client->parser
0
- , client->parser.nread
0
- if(http_parser_has_error(&client->parser)) goto error;
0
- if(total_request_size == client->read) {
0
- ev_io_stop(loop, watcher);
0
- client->request_body = client->read_buffer + client->parser.nread;
0
- client->nread_from_body = 0;
0
- if(http_parser_is_finished(&client->parser) && total_request_size > EBB_BUFFERSIZE ) {
0
- /* read body into file - in a thread */
0
+ client->request_buffer[client->read] = '\0'; /* make ragel happy */
0
+ http_parser_execute( &client->parser
0
+ , client->request_buffer
0
+ , client->parser.nread
0
+ if(http_parser_has_error(&client->parser)) goto error;
0
+ if(http_parser_is_finished(&client->parser)) {
0
ev_io_stop(loop, watcher);
0
- assert(0 <= pthread_create(&thread, NULL, read_body_into_file, client));
0
- pthread_join(thread, NULL);
0
+ client->read_from_body = 0;
0
+ client->request_body_head = client->request_buffer + client->parser.nread;
0
+ client->request_body_head_size = client->read - client->parser.nread;
0
@@ -372,8 +291,8 @@ void on_request( struct ev_loop *loop
0
client->write_buffer->len = 0; // see note in ebb_client_close
0
client->content_length = 0;
0
- client->request_body = NULL;
0
- client->nread_from_body = -1;
0
+ client->request_body_head = NULL;
0
+ client->request_body_head_size = 0;
0
/* SETUP READ AND TIMEOUT WATCHERS */
0
client->read_watcher.data = client;
0
@@ -494,10 +413,6 @@ void ebb_client_close(ebb_client *client)
0
ev_io_stop(client->server->loop, &(client->write_watcher));
0
ev_timer_stop(client->server->loop, &(client->timeout_watcher));
0
- if(client->upload_file) {
0
- fclose(client->upload_file);
0
- unlink(client->upload_file_filename);
0
http_parser_finish(&(client->parser));
0
@@ -558,13 +473,51 @@ void ebb_client_write(ebb_client *client, const char *data, int length)
0
+/* pass an allocated buffer and the length to read. this function will try to
0
+ * fill the buffer with that length of data read from the body of the request.
0
+ * the return value says how much was actually written.
0
+size_t ebb_client_read(ebb_client *client, char *buffer, int length)
0
+ assert(TRUE == http_parser_is_finished(&client->parser));
0
+ assert(client->read_from_body <= client->content_length);
0
+ size_t to_read = ramp(min(length, client->content_length - client->read_from_body));
0
+ if(client->read_from_body < client->request_body_head_size) {
0
+ , client->request_body_head + client->read_from_body
0
+ client->read_from_body += to_read;
0
+ ssize_t read = recv(client->fd, buffer, to_read, 0);
0
+ ebb_warning("closing client. ebb_client_read: %s", strerror(errno));
0
+ ebb_client_close(client);
0
+ client->read_from_body += read;
0
void ebb_client_finished( ebb_client *client)
0
assert(FALSE == ev_is_active(&(client->write_watcher)));
0
+ /* assure the socket is still in non-blocking mode
0
+ * in the ruby binding, for example, i change this flag
0
int flags = fcntl(client->fd, F_GETFL, 0);
0
- assert(0 <= fcntl(client->fd, F_SETFL, flags | O_NONBLOCK));
0
+ if(0 > fcntl(client->fd, F_SETFL, flags | O_NONBLOCK)) {
0
+ ebb_client_close(client);
0
client->write_watcher.data = client;
0
@@ -574,31 +527,6 @@ void ebb_client_finished( ebb_client *client)
0
-/* pass an allocated buffer and the length to read. this function will try to
0
- * fill the buffer with that length of data read from the body of the request.
0
- * the return value says how much was actually written.
0
-size_t ebb_client_read(ebb_client *client, char *buffer, int length)
0
- if(client->upload_file) {
0
- read = fread(buffer, 1, length, client->upload_file);
0
- /* TODO error checking! */
0
- read = ramp(min(length, client->content_length - client->nread_from_body));
0
- , client->request_body + client->nread_from_body
0
- client->nread_from_body += read;
0
/* The following socket creation routines are modified and stolen from memcached */
0
static int server_socket(const int port) {
Comments
No one has commented yet.