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