/* * http_prepare_response() * * Service the various HTTP commands, calling the relevant subroutine. * We only handle GET and POST. */ int http_prepare_response(http_conn* conn) { int ret_code = 0; switch (conn->action) { case GET: { /* Find file from uri */ ret_code = http_find_file(conn); break; } case POST: { /* Handle POSTs. */ ret_code = http_handle_post(conn); break; } default: { break; } } /* switch (conn->action) */ return ret_code; }
/* ProgFlashStub() * * A thin wrapper around the ProgSRECBuf() function in srec_flash.c. * */ void ProgFlashStub(http_conn* conn) { struct flash_inf_struct *flash_info = &flash_inf; /* Call ParseSRECBuf, with the flash_info argument. */ ParseSRECBuf( flash_info ); /* Go find and send the reset_system.html file. */ http_find_file( conn ); /* Close the connection. */ conn->close = 1; return; }
/* * http_handle_get() * * Handle incoming get requests. Fall back to default handling if URL is not known */ int http_handle_get(http_conn* conn){ //Check conn->url for known uris int len = 0; bool isImage = false; const char * retval = httpResponseFunction(conn->uri, &len, &isImage); if (len > 0){ http_send_header(conn, len, HTTP_OK, isImage); //Send JSON reply send(conn->fd, (void*)retval, len, 0); return 0; } else { return http_find_file(conn); } }
void file_upload(http_conn* conn) { int buf_len; int data_used; struct upload_buf_struct *upload_buffer = &upload_buf; struct flash_inf_struct *flash_info = &flash_inf; /* Look for boundary, parse multipart form "mini" header information if found. */ if( strstr( conn->rx_rd_pos, conn->boundary ) ) { if( http_parse_multipart_header( conn ) ) { printf( "multipart-form: header parse failure...resetting connection!" ); conn->state = RESET; } } /* Exception for IE. It sometimes sends _really_ small initial packets! */ if( strchr( conn->rx_rd_pos, ':' ) ) { conn->state = READY; return; } /* Calculate the string size... */ buf_len = strlen(conn->rx_rd_pos); conn->content_received = conn->content_received + buf_len; /* Copy all the received data into the upload buffer. */ if ( memcpy( (void*) upload_buffer->wr_pos, (void*) conn->rx_rd_pos, buf_len ) == NULL ) { printf( "ERROR: memcpy to file upload buffer failed!" ); } /* Increment the wr_pos pointer to just after the received data. */ upload_buffer->wr_pos = upload_buffer->wr_pos + buf_len; conn->rx_rd_pos = conn->rx_rd_pos + buf_len; /* Reset the buffers after copying the data into the big intermediate * buffer.*/ data_used = conn->rx_rd_pos - conn->rx_buffer; memmove(conn->rx_buffer,conn->rx_rd_pos,conn->rx_wr_pos-conn->rx_rd_pos); conn->rx_rd_pos = conn->rx_buffer; conn->rx_wr_pos -= data_used; memset(conn->rx_wr_pos, 0, data_used); if ( conn->file_upload == 0 ) { printf( "Received a total of %d bytes.\n", conn->content_received ); /* Insert a NULL character (temporarily). */ *upload_buffer->wr_pos = '\0'; /* Populate flash_info struct... print the buffer size. */ flash_info->size = (int) strlen(upload_buffer->buffer); printf( "Upload Buffer size = %d.\n", flash_info->size); strcpy( flash_info->device, conn->flash_device ); flash_info->start = upload_buffer->rd_pos; /* Populate the flash_inf struct. */ //printf( "Here's the Buffer:\n\n%s", upload_buffer->buffer); http_find_file(conn); conn->close = 1; } else { conn->state = READY; } }