diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | cgi.c | 68 |
2 files changed, 29 insertions, 41 deletions
@@ -15,5 +15,5 @@ clean: | |||
15 | rm -f ./main | 15 | rm -f ./main |
16 | 16 | ||
17 | %: | 17 | %: |
18 | $(CC) $(CFLAGS) -o $@ *.c $(LIBS) | 18 | $(CC) $(CFLAGS) -o $@ cgi.c main.c $(LIBS) |
19 | 19 | ||
@@ -38,14 +38,6 @@ | |||
38 | #include "main.h" | 38 | #include "main.h" |
39 | #include <cups/cups.h> | 39 | #include <cups/cups.h> |
40 | 40 | ||
41 | # ifdef __FreeBSD__ | ||
42 | # include <sys/types.h> | ||
43 | # include <sys/socket.h> | ||
44 | # include <sys/uio.h> | ||
45 | # else | ||
46 | # include <sys/sendfile.h> | ||
47 | # endif | ||
48 | |||
49 | static const char * line1 = NULL, * line2 = NULL; | 41 | static const char * line1 = NULL, * line2 = NULL; |
50 | 42 | ||
51 | int print_on_correct_printer(void * user_data, unsigned flags, cups_dest_t * dest) { | 43 | 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 | |||
89 | return 0; | 81 | return 0; |
90 | } | 82 | } |
91 | 83 | ||
92 | void send_answer(Http_Header * http_header, int fd_socket) { | 84 | void send_answer_file(Http_Header * http_header, int fd_socket) { |
85 | |||
93 | FILE * f = fdopen((size_t) fd_socket, "w"); | 86 | FILE * f = fdopen((size_t) fd_socket, "w"); |
94 | fputs("HTTP/1.0 200 OK\n", f); | 87 | fputs("HTTP/1.0 200 OK\n", f); |
95 | fputs("content-type: text/html\n\n", f); | 88 | fputs("content-type: text/html\n\n", f); |
96 | fflush(f); | 89 | fflush(f); |
97 | 90 | ||
91 | #define BUFFER_SIZE 1024 | ||
92 | char buffer[BUFFER_SIZE]; | ||
93 | FILE * file = fopen(&http_header->url[1], "r"); | ||
94 | if(file) { | ||
95 | size_t read_size = 0; | ||
96 | for(;;) { | ||
97 | read_size = fread(buffer, 1, BUFFER_SIZE, file); | ||
98 | fwrite(buffer, 1, read_size, f); | ||
99 | fflush(f); | ||
100 | if(feof(file)) | ||
101 | break; | ||
102 | } | ||
103 | } else { | ||
104 | if(http_header->url) // TODO: too dangerous to check that here, that is too late. | ||
105 | fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]); | ||
106 | } | ||
107 | |||
108 | fflush(f); | ||
109 | fclose(f); | ||
110 | return; | ||
111 | } | ||
112 | |||
113 | void send_answer(Http_Header * http_header, int fd_socket) { | ||
114 | |||
98 | char * printer_name = "Brother_QL-720NW"; | 115 | char * printer_name = "Brother_QL-720NW"; |
99 | cupsEnumDests(CUPS_DEST_FLAGS_NONE, | 116 | cupsEnumDests(CUPS_DEST_FLAGS_NONE, |
100 | 0, | 117 | 0, |
@@ -104,42 +121,13 @@ void send_answer(Http_Header * http_header, int fd_socket) { | |||
104 | print_on_correct_printer, | 121 | print_on_correct_printer, |
105 | printer_name | 122 | printer_name |
106 | ); | 123 | ); |
107 | |||
108 | line1 = NULL; | 124 | line1 = NULL; |
109 | line2 = NULL; | 125 | line2 = NULL; |
110 | 126 | ||
111 | int file = open("index.html", O_RDONLY); | 127 | http_header->url = "/index.html"; |
112 | if(0 < file) { | 128 | send_answer_file(http_header, fd_socket); |
113 | struct stat stat; | ||
114 | fstat(file, &stat); | ||
115 | sendfile (fileno(f), file, NULL, stat.st_size); | ||
116 | } else { | ||
117 | if(http_header->url) // TODO: too dangerous to check that here, that is too late. | ||
118 | fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]); | ||
119 | } | ||
120 | |||
121 | } | 129 | } |
122 | 130 | ||
123 | void send_answer_file(Http_Header * http_header, int fd_socket) { | ||
124 | |||
125 | FILE * f = fdopen((size_t) fd_socket, "w"); | ||
126 | fputs("HTTP/1.0 200 OK\n", f); | ||
127 | fputs("content-type: text/plain\n\n", f); | ||
128 | fflush(f); | ||
129 | |||
130 | int file = open(&http_header->url[1], O_RDONLY); | ||
131 | if(0 < file) { | ||
132 | struct stat stat; | ||
133 | fstat(file, &stat); | ||
134 | sendfile (fileno(f), file, NULL, stat.st_size); | ||
135 | } else { | ||
136 | if(http_header->url) // TODO: too dangerous to check that here, that is too late. | ||
137 | fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]); | ||
138 | } | ||
139 | |||
140 | fclose(f); | ||
141 | return; | ||
142 | } | ||
143 | 131 | ||
144 | void next_part(Http_Header * http_header, const char * content, size_t content_size) { | 132 | void next_part(Http_Header * http_header, const char * content, size_t content_size) { |
145 | if(!http_header->content_disposition) | 133 | if(!http_header->content_disposition) |