void cgi_http_write_file(cgi_http_connection_t *connection, char *fpath) { char buffer[CGI_NAME_BUFFER_SIZE]; snprintf(buffer, CGI_NAME_BUFFER_SIZE - 1, "%s%s", CGI_WEB_ROOT, fpath); connection->ffd = open(buffer, O_RDONLY); if (connection->ffd == -1) { perror("open"); } else { connection->fsize = lseek(connection->ffd, 0, SEEK_END); lseek(connection->ffd, 0, SEEK_SET); if (connection->fsize == -1) { perror("lseek"); } else { snprintf(buffer, CGI_NAME_BUFFER_SIZE - 1, "%" PRIu64 , connection->fsize); cgi_http_write_header(connection, "Content-Length", buffer); if(connection->linger == 1) { cgi_http_write_header(connection, "Connection", "keep-alive"); snprintf(buffer, CGI_NAME_BUFFER_SIZE - 1, "timeout=%" PRIu32, (connection->dispatcher->connection_timeout/1000)); cgi_http_write_header(connection, "Keep-Alive", buffer); } connection->wbuffer[connection->write_idx++] = '\r'; connection->wbuffer[connection->write_idx++] = '\n'; if (lseek(connection->ffd, 0, SEEK_SET) == -1) { perror("lseek"); } /*** Another way : write in one tcp segment ***/ /* char buffer1[1024]; int numbytes; while((numbytes = read(connection->ffd, buffer1, 1024)) > 0){ connection->write_idx += snprintf(connection->wbuffer + connection->write_idx, numbytes, "%s", buffer1); } close(connection->ffd); */ } } }
void cgi_http_write_file(cgi_http_connection_t *connection, char *fpath) { char buffer[CGI_NAME_BUFFER_SIZE]; snprintf(buffer, CGI_NAME_BUFFER_SIZE - 1, "%s%s", CGI_WEB_ROOT, fpath); connection->ffd = open(buffer, O_RDONLY); if (connection->ffd == -1) { perror("open"); } else { connection->fsize = lseek(connection->ffd, 0, SEEK_END); if (connection->fsize == -1) { perror("lseek"); } else { snprintf(buffer, CGI_NAME_BUFFER_SIZE - 1, "%lu", connection->fsize); cgi_http_write_header(connection, "Content-Length", buffer); connection->wbuffer[connection->write_idx++] = '\r'; connection->wbuffer[connection->write_idx++] = '\n'; if (lseek(connection->ffd, 0, SEEK_SET) == -1) { perror("lseek"); } } } }