summaryrefslogtreecommitdiff
path: root/cgi.c
diff options
context:
space:
mode:
authorMax Christian Pohle2021-11-21 00:10:29 +0100
committerMax Christian Pohle2021-11-21 00:10:29 +0100
commitb7b47e662a75096c7f99881b0d609731d5d6c8ce (patch)
tree113da9bbb5335f6ba48f08029c9e5a050d6304b7 /cgi.c
parentf5e12f35af533713312f29054ad0ffd00c0fcabd (diff)
downloadohmycgi-b7b47e662a75096c7f99881b0d609731d5d6c8ce.tar.bz2
ohmycgi-b7b47e662a75096c7f99881b0d609731d5d6c8ce.zip
First working version
TODO: needs refactoring
Diffstat (limited to 'cgi.c')
-rw-r--r--cgi.c154
1 files changed, 154 insertions, 0 deletions
diff --git a/cgi.c b/cgi.c
new file mode 100644
index 0000000..76751fa
--- /dev/null
+++ b/cgi.c
@@ -0,0 +1,154 @@
1/* __ _
2* ____ / /_ ____ ___ __ ___________ _(_)
3* / __ \/ __ \/ __ `__ \/ / / / ___/ __ `/ /
4* / /_/ / / / / / / / / / /_/ / /__/ /_/ / /
5* \____/_/ /_/_/ /_/ /_/\__, /\___/\__, /_/
6* /____/ /____/
7*
8* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
9*
10* Copyright (c) 2021, Max Christian Pohle <max@coderonline.de>
11*
12* Redistribution and use in source and binary forms, with or without
13* modification, are permitted provided that the following conditions
14* are met:
15*
16* 1. Redistributions of source code must retain the above copyright
17* notice, this list of conditions and the following disclaimer.
18*
19* 2. Redistributions in binary form must reproduce the above copyright
20* notice, this list of conditions and the following disclaimer in the
21* documentation and/or other materials provided with the distribution.
22*
23* {{{ DISCLAIMER
24* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34* POSSIBILITY OF SUCH DAMAGE.
35* }}}
36*/
37
38#include "main.h"
39#include <cups/cups.h>
40
41static const char * line1 = NULL, * line2 = NULL;
42
43int print_on_correct_printer(void * user_data, unsigned flags, cups_dest_t * dest) {
44 if(!strstr(dest->name, user_data)) {
45 printf("> Wrong name: %s continue search\n", dest->name);
46 return 1;
47 }
48
49 if(!dest || !line1 || !line2) {
50 printf("FAILED: %p %p %p\n", dest, line1, line2);
51 return 1;
52 }
53
54 int job_id = 0;
55 int num_options = 0;
56
57 cups_option_t * options = NULL;
58 num_options = cupsAddOption(CUPS_MEDIA_TYPE, CUPS_MEDIA_TYPE_LABELS, num_options, &options);
59 num_options = cupsAddOption(CUPS_MEDIA, CUPS_MEDIA_SOURCE_MANUAL, num_options, &options);
60 num_options = cupsAddOption(CUPS_PRINT_QUALITY, CUPS_PRINT_QUALITY_HIGH, num_options, &options);
61 num_options = cupsAddOption(CUPS_MEDIA, "62x15mm", num_options, &options);
62
63 cups_dinfo_t * info = cupsCopyDestInfo(CUPS_HTTP_DEFAULT, dest);
64
65 if (cupsCreateDestJob(CUPS_HTTP_DEFAULT, dest, info, &job_id, "My Document", num_options, options) == IPP_STATUS_OK)
66 printf("Created job: %d\n", job_id);
67 else
68 printf("Unable to create job: %s\n", cupsLastErrorString());
69
70 if (cupsStartDestDocument(CUPS_HTTP_DEFAULT, dest, info, job_id, "filename.pdf", CUPS_FORMAT_TEXT, num_options, options, 1) == HTTP_STATUS_CONTINUE)
71 {
72 cupsWriteRequestData(CUPS_HTTP_DEFAULT, line1, strlen(line1));
73 cupsWriteRequestData(CUPS_HTTP_DEFAULT, "\n", 1);
74 cupsWriteRequestData(CUPS_HTTP_DEFAULT, line2, strlen(line2));
75
76 if (cupsFinishDestDocument(CUPS_HTTP_DEFAULT, dest, info) == IPP_STATUS_OK)
77 puts("Document send succeeded.");
78 else
79 printf("Document send failed: %s\n", cupsLastErrorString());
80 }
81 return 0;
82}
83
84void send_answer(Http_Header * http_header, int fd_socket) {
85 FILE * f = fdopen((size_t) fd_socket, "w");
86 fputs("HTTP/1.0 200 OK\n", f);
87 fputs("content-type: text/html\n\n", f);
88 fflush(f);
89
90 char * printer_name = "Brother_QL-720NW";
91 cupsEnumDests(CUPS_DEST_FLAGS_NONE,
92 0,
93 NULL,
94 CUPS_PRINTER_VARIABLE,
95 CUPS_PRINTER_LOCAL,
96 print_on_correct_printer,
97 printer_name
98 );
99
100 line1 = NULL;
101 line2 = NULL;
102
103 int file = open("index.html", O_RDONLY);
104 if(0 < file) {
105 struct stat stat;
106 fstat(file, &stat);
107 sendfile (fileno(f), file, NULL, stat.st_size);
108 } else {
109 if(http_header->url) // TODO: too dangerous to check that here, that is too late.
110 fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]);
111 }
112
113}
114
115void send_answer_file(Http_Header * http_header, int fd_socket) {
116
117 FILE * f = fdopen((size_t) fd_socket, "w");
118 fputs("HTTP/1.0 200 OK\n", f);
119 fputs("content-type: text/plain\n\n", f);
120 fflush(f);
121
122 int file = open(&http_header->url[1], O_RDONLY);
123 if(0 < file) {
124 struct stat stat;
125 fstat(file, &stat);
126 sendfile (fileno(f), file, NULL, stat.st_size);
127 } else {
128 if(http_header->url) // TODO: too dangerous to check that here, that is too late.
129 fprintf(f, "could not open file \"%s\"\n", &http_header->url[1]);
130 }
131
132 fclose(f);
133 return;
134}
135
136void next_part(Http_Header * http_header, const char * content, size_t content_size) {
137 if(!http_header->content_disposition)
138 return;
139
140 if(strstr(http_header->content_disposition, "name=\"line1\"")) {
141 line1 = content;
142 } else if(strstr(http_header->content_disposition, "name=\"line2\"")) {
143 line2 = content;
144 }
145
146 printf("> content_disposition: %s\n", http_header->content_disposition);
147 printf("> size: %ld\n", content_size);
148 fwrite(content, content_size, 1, stdout);
149 puts("");
150 fflush(stdout);
151 return;
152}
153
154// modeline for vim: shiftwidth=2 tabstop=2 number foldmethod=marker
..