int http_serve_file ( http_request *h, const gchar *docroot ) { gchar *path; guint fd, status; path = http_fix_path( h->uri, docroot ); fd = http_open_file( path, &status ); http_add_header( h, "Content-Type", http_mime_type( path ) ); http_send_header( h, status, fd == -1 ? "Not OK" : "OK" ); if ( fd != -1 ) http_sendfile( h, fd ); close(fd); g_free(path); return ( fd != -1 ); }
void wget_callback(uint16_t ev, uint16_t conn) { static char data[1024 * 128]; /* Buffer: 128kb */ static uint32_t _length = 0u; static uint32_t _length_tot = 0u; static uint32_t start_time = 0u; static int fd = -1; if(ev & EV_HTTP_CON) { printf("Connected to the download server\n"); start_time = PICO_TIME_MS(); pico_http_client_sendHeader(conn, NULL, HTTP_HEADER_DEFAULT); _length = 0u; } if(ev & EV_HTTP_REQ) { struct pico_http_header *header = pico_http_client_readHeader(conn); printf("Received header from server...\n"); printf("Server response : %d\n", header->responseCode); printf("Location : %s\n", header->location); printf("Transfer-Encoding : %d\n", header->transferCoding); printf("Size/Chunk : %d\n", header->contentLengthOrChunk); fd = http_open_file(); } if(ev & EV_HTTP_BODY) { int len; struct pico_http_header *header = pico_http_client_readHeader(conn); printf("Reading data... len=%d\n", _length_tot + _length); if (_length + 1024 >= sizeof(data)) { http_save_file(fd, data, _length); _length_tot += _length; _length = 0u; } /* Read from buffer */ while((len = pico_http_client_readData(conn, data + _length, 1024)) && len > 0) { _length += (uint32_t)len; } if(header->contentLengthOrChunk == _length) ev = EV_HTTP_CLOSE; } if(ev & EV_HTTP_CLOSE) { struct pico_http_header *header = pico_http_client_readHeader(conn); int len; uint32_t speed; printf("Connection was closed...\n"); printf("Reading remaining data, if any ...\n"); if(!header) { printf("No header received\n"); pico_http_client_close(conn); } /* first save any open read bytes */ http_save_file(fd, data, _length); _length_tot += _length; _length = 0u; while((len = pico_http_client_readData(conn, data + _length, 1000u)) && len > 0) { _length += (uint32_t)len; } printf("Read a total data of : %d bytes \n", _length_tot); if(header->transferCoding == HTTP_TRANSFER_CHUNKED) { if(header->contentLengthOrChunk) { printf("Last chunk data not fully read !\n"); exit(1); } else { printf("Transfer ended with a zero chunk! OK !\n"); } } else { if(header->contentLengthOrChunk == (_length + _length_tot)) { printf("Received the full : %d \n", _length + _length_tot); } else { printf("Received %d , waiting for %d\n", _length + _length_tot, header->contentLengthOrChunk); } } if (!url_filename) { printf("Failed to get local filename\n"); } len = http_save_file(fd, data, _length); http_close_file(fd); if ((len < 0) || ((uint32_t)len < _length)) { printf("Failed to save file: %s\n", strerror(errno)); } speed = _length_tot / (PICO_TIME_MS() - start_time) * 8; printf("Download speed: %d kbps\n", speed); pico_http_client_close(conn); pico_free(url_filename); } if(ev & EV_HTTP_ERROR) { printf("Connection error (probably dns failed : check the routing table), trying to close the client...\n"); pico_http_client_close(conn); } if(ev & EV_HTTP_DNS) { printf("The DNS query was successful ... \n"); } }