From 00cca3608962ef1f2ca9672024671221095df54c Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Tue, 23 Nov 2021 19:08:26 +0100 Subject: Removed sendfile for now It is not compatible between linux and freebsd (different number of parameters) --- Makefile | 2 +- cgi.c | 68 ++++++++++++++++++++++++++-------------------------------------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index b15cafb..b352ab3 100644 --- a/Makefile +++ b/Makefile @@ -15,5 +15,5 @@ clean: rm -f ./main %: - $(CC) $(CFLAGS) -o $@ *.c $(LIBS) + $(CC) $(CFLAGS) -o $@ cgi.c main.c $(LIBS) diff --git a/cgi.c b/cgi.c index 9f75399..aa86c9a 100644 --- a/cgi.c +++ b/cgi.c @@ -38,14 +38,6 @@ #include "main.h" #include -# ifdef __FreeBSD__ -# include -# include -# include -# else -# include -# endif - static const char * line1 = NULL, * line2 = NULL; int print_on_correct_printer(void * user_data, unsigned flags, cups_dest_t * dest) { @@ -89,12 +81,37 @@ int print_on_correct_printer(void * user_data, unsigned flags, cups_dest_t * des return 0; } -void send_answer(Http_Header * http_header, int fd_socket) { +void send_answer_file(Http_Header * http_header, int fd_socket) { + FILE * f = fdopen((size_t) fd_socket, "w"); fputs("HTTP/1.0 200 OK\n", f); fputs("content-type: text/html\n\n", f); fflush(f); + #define BUFFER_SIZE 1024 + char buffer[BUFFER_SIZE]; + FILE * file = fopen(&http_header->url[1], "r"); + if(file) { + size_t read_size = 0; + for(;;) { + read_size = fread(buffer, 1, BUFFER_SIZE, file); + fwrite(buffer, 1, read_size, f); + fflush(f); + if(feof(file)) + break; + } + } else { + if(http_header->url) // TODO: too dangerous to check that here, that is too late. + fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]); + } + + fflush(f); + fclose(f); + return; +} + +void send_answer(Http_Header * http_header, int fd_socket) { + char * printer_name = "Brother_QL-720NW"; cupsEnumDests(CUPS_DEST_FLAGS_NONE, 0, @@ -104,42 +121,13 @@ void send_answer(Http_Header * http_header, int fd_socket) { print_on_correct_printer, printer_name ); - line1 = NULL; line2 = NULL; - int file = open("index.html", O_RDONLY); - if(0 < file) { - struct stat stat; - fstat(file, &stat); - sendfile (fileno(f), file, NULL, stat.st_size); - } else { - if(http_header->url) // TODO: too dangerous to check that here, that is too late. - fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]); - } - + http_header->url = "/index.html"; + send_answer_file(http_header, fd_socket); } -void send_answer_file(Http_Header * http_header, int fd_socket) { - - FILE * f = fdopen((size_t) fd_socket, "w"); - fputs("HTTP/1.0 200 OK\n", f); - fputs("content-type: text/plain\n\n", f); - fflush(f); - - int file = open(&http_header->url[1], O_RDONLY); - if(0 < file) { - struct stat stat; - fstat(file, &stat); - sendfile (fileno(f), file, NULL, stat.st_size); - } else { - if(http_header->url) // TODO: too dangerous to check that here, that is too late. - fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]); - } - - fclose(f); - return; -} void next_part(Http_Header * http_header, const char * content, size_t content_size) { if(!http_header->content_disposition) -- cgit v1.2.3