-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
client-webif.c
549 lines (444 loc) · 12.5 KB
/
client-webif.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//
// Core client web interface functions for the Printer Application Framework
//
// Copyright © 2019-2020 by Michael R Sweet.
// Copyright © 2010-2019 by Apple Inc.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Include necessary headers...
//
#include "pappl-private.h"
//
// 'papplClientGetForm()' - Get POST form data from the web client.
//
int // O - Number of form variables read
papplClientGetForm(
pappl_client_t *client, // I - Client
cups_option_t **form) // O - Form variables
{
int num_form = 0; // Number of form variables
char body[8192], // Message body data string
*ptr, // Pointer into string
name[64], // Variable name
*nameptr, // Pointer into name
value[1024], // Variable value
*valptr; // Pointer into value
ssize_t bytes; // Bytes read
http_state_t initial_state; // Initial HTTP state
// Read form data...
initial_state = httpGetState(client->http);
for (ptr = body; ptr < (body + sizeof(body) - 1); ptr += bytes)
{
if ((bytes = httpRead2(client->http, ptr, sizeof(body) - (size_t)(ptr - body + 1))) <= 0)
break;
}
*ptr = '\0';
if (httpGetState(client->http) == initial_state)
httpFlush(client->http); // Flush remainder...
// Parse the form data...
*form = NULL;
for (ptr = body; *ptr;)
{
// Get the name...
nameptr = name;
while (*ptr && *ptr != '=')
{
int ch = *ptr++; // Name character
if (ch == '%' && isxdigit(ptr[0] & 255) && isxdigit(ptr[1] & 255))
{
// Hex-encoded character
if (isdigit(*ptr))
ch = (*ptr++ - '0') << 4;
else
ch = (tolower(*ptr++) - 'a' + 10) << 4;
if (isdigit(*ptr))
ch |= *ptr++ - '0';
else
ch |= tolower(*ptr++) - 'a' + 10;
}
else if (ch == '+')
ch = ' ';
if (nameptr < (name + sizeof(name) - 1))
*nameptr++ = ch;
}
*nameptr = '\0';
if (!*ptr)
break;
// Get the value...
ptr ++;
valptr = value;
while (*ptr && *ptr != '&')
{
int ch = *ptr++; // Name character
if (ch == '%' && isxdigit(ptr[0] & 255) && isxdigit(ptr[1] & 255))
{
// Hex-encoded character
if (isdigit(*ptr))
ch = (*ptr++ - '0') << 4;
else
ch = (tolower(*ptr++) - 'a' + 10) << 4;
if (isdigit(*ptr))
ch |= *ptr++ - '0';
else
ch |= tolower(*ptr++) - 'a' + 10;
}
else if (ch == '+')
ch = ' ';
if (valptr < (value + sizeof(value) - 1))
*valptr++ = ch;
}
*valptr = '\0';
if (*ptr)
ptr ++;
// Add the name + value to the option array...
num_form = cupsAddOption(name, value, num_form, form);
}
return (num_form);
}
//
// 'papplClientHTMLButton()' - Show a HTML button.
//
void
papplClientHTMLButton(
pappl_client_t *client, // I - Client
const char *label, // I - Button label
const char *href, // I - Link
bool require_login) // I - `true` if the linked page requires a login, `false` otherwise
{
char uri[1024]; // New link
// Range check input...
if (!client || !label || !href)
return;
if (!client->username[0] && require_login)
{
// Need to redirect the link to the login page...
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "https", NULL, client->host_field, client->host_port, "/login?page=%s", href);
href = uri;
}
papplClientHTMLPrintf(client, "<a class=\"btn\" href=\"%s\">%s</a>", href, label);
}
//
// 'papplClientHTMLEscape()' - Write a HTML-safe string.
//
void
papplClientHTMLEscape(
pappl_client_t *client, // I - Client
const char *s, // I - String to write
size_t slen) // I - Number of characters to write (`0` for nul-terminated)
{
const char *start, // Start of segment
*end; // End of string
start = s;
end = s + (slen > 0 ? slen : strlen(s));
while (*s && s < end)
{
if (*s == '&' || *s == '<')
{
if (s > start)
httpWrite2(client->http, start, (size_t)(s - start));
if (*s == '&')
httpWrite2(client->http, "&", 5);
else
httpWrite2(client->http, "<", 4);
start = s + 1;
}
s ++;
}
if (s > start)
httpWrite2(client->http, start, (size_t)(s - start));
}
//
// 'papplClientHTMLFooter()' - Show the web interface footer.
//
// This function also writes the trailing 0-length chunk.
//
void
papplClientHTMLFooter(
pappl_client_t *client) // I - Client
{
const char *footer = papplSystemGetFooterHTML(papplClientGetSystem(client));
// Footer HTML
if (footer)
{
papplClientHTMLPuts(client,
" <div class=\"footer\">\n"
" <div class=\"row\">\n"
" <div class=\"col-12\">");
papplClientHTMLPuts(client, footer);
papplClientHTMLPuts(client,
"</div>\n"
" </div>\n"
" </div>\n");
}
papplClientHTMLPuts(client,
" </body>\n"
"</html>\n");
httpWrite2(client->http, "", 0);
}
//
// 'papplClientHTMLHeader()' - Show the web interface header and title.
//
void
papplClientHTMLHeader(
pappl_client_t *client, // I - Client
const char *title, // I - Title
int refresh) // I - Refresh timer, if any
{
pappl_system_t *system = client->system;
// System
pappl_printer_t *printer; // Printer
_pappl_resource_t *r; // Current resource
const char *name; // Name for title/header
if ((system->options & PAPPL_SOPTIONS_MULTI_QUEUE) || (printer = (pappl_printer_t *)cupsArrayFirst(system->printers)) == NULL)
name = system->name;
else
name = printer->name;
papplClientHTMLPrintf(client,
"<!DOCTYPE html>\n"
"<html>\n"
" <head>\n"
" <title>%s - %s</title>\n"
" <link rel=\"shortcut icon\" href=\"/favicon.png\" type=\"image/png\">\n"
" <link rel=\"stylesheet\" href=\"/style.css\">\n"
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\">\n", title, name);
if (refresh > 0)
papplClientHTMLPrintf(client, "<meta http-equiv=\"refresh\" content=\"%d\">\n", refresh);
papplClientHTMLPrintf(client,
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
" </head>\n"
" <body>\n"
" <div class=\"header\">\n"
" <div class=\"row\">\n"
" <div class=\"col-12 nav\">\n"
" <a class=\"btn\" href=\"/\"><img src=\"%s\"></a>\n", (system->options & PAPPL_SOPTIONS_MULTI_QUEUE) ? "/navicon.png" : "/icon-sm.png");
pthread_rwlock_rdlock(&system->rwlock);
for (r = (_pappl_resource_t *)cupsArrayFirst(system->resources); r; r = (_pappl_resource_t *)cupsArrayNext(system->resources))
{
if (r->label)
papplClientHTMLPrintf(client, " <a class=\"btn\" href=\"%s\">%s</a>\n", r->path, r->label);
}
pthread_rwlock_unlock(&system->rwlock);
papplClientHTMLPuts(client,
" </div>\n"
" </div>\n"
" </div>\n");
}
//
// 'papplClientHTMLPrintf()' - Send formatted text to the client, quoting as needed.
//
void
papplClientHTMLPrintf(
pappl_client_t *client, // I - Client
const char *format, // I - Printf-style format string
...) // I - Additional arguments as needed
{
va_list ap; // Pointer to arguments
const char *start; // Start of string
char size, // Size character (h, l, L)
type; // Format type character
int width, // Width of field
prec; // Number of characters of precision
char tformat[100], // Temporary format string for sprintf()
*tptr, // Pointer into temporary format
temp[1024]; // Buffer for formatted numbers
char *s; // Pointer to string
// Loop through the format string, formatting as needed...
va_start(ap, format);
start = format;
while (*format)
{
if (*format == '%')
{
if (format > start)
httpWrite2(client->http, start, (size_t)(format - start));
tptr = tformat;
*tptr++ = *format++;
if (*format == '%')
{
httpWrite2(client->http, "%", 1);
format ++;
start = format;
continue;
}
else if (strchr(" -+#\'", *format))
*tptr++ = *format++;
if (*format == '*')
{
// Get width from argument...
format ++;
width = va_arg(ap, int);
snprintf(tptr, sizeof(tformat) - (size_t)(tptr - tformat), "%d", width);
tptr += strlen(tptr);
}
else
{
width = 0;
while (isdigit(*format & 255))
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
width = width * 10 + *format++ - '0';
}
}
if (*format == '.')
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
format ++;
if (*format == '*')
{
// Get precision from argument...
format ++;
prec = va_arg(ap, int);
snprintf(tptr, sizeof(tformat) - (size_t)(tptr - tformat), "%d", prec);
tptr += strlen(tptr);
}
else
{
prec = 0;
while (isdigit(*format & 255))
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
prec = prec * 10 + *format++ - '0';
}
}
}
if (*format == 'l' && format[1] == 'l')
{
size = 'L';
if (tptr < (tformat + sizeof(tformat) - 2))
{
*tptr++ = 'l';
*tptr++ = 'l';
}
format += 2;
}
else if (*format == 'h' || *format == 'l' || *format == 'L')
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
size = *format++;
}
else
size = 0;
if (!*format)
{
start = format;
break;
}
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
type = *format++;
*tptr = '\0';
start = format;
switch (type)
{
case 'E' : // Floating point formats
case 'G' :
case 'e' :
case 'f' :
case 'g' :
if ((size_t)(width + 2) > sizeof(temp))
break;
sprintf(temp, tformat, va_arg(ap, double));
httpWrite2(client->http, temp, strlen(temp));
break;
case 'B' : // Integer formats
case 'X' :
case 'b' :
case 'd' :
case 'i' :
case 'o' :
case 'u' :
case 'x' :
if ((size_t)(width + 2) > sizeof(temp))
break;
# ifdef HAVE_LONG_LONG
if (size == 'L')
sprintf(temp, tformat, va_arg(ap, long long));
else
# endif // HAVE_LONG_LONG
if (size == 'l')
sprintf(temp, tformat, va_arg(ap, long));
else
sprintf(temp, tformat, va_arg(ap, int));
httpWrite2(client->http, temp, strlen(temp));
break;
case 'p' : // Pointer value
if ((size_t)(width + 2) > sizeof(temp))
break;
sprintf(temp, tformat, va_arg(ap, void *));
httpWrite2(client->http, temp, strlen(temp));
break;
case 'c' : // Character or character array
if (width <= 1)
{
temp[0] = (char)va_arg(ap, int);
temp[1] = '\0';
papplClientHTMLEscape(client, temp, 1);
}
else
papplClientHTMLEscape(client, va_arg(ap, char *), (size_t)width);
break;
case 's' : // String
if ((s = va_arg(ap, char *)) == NULL)
s = "(null)";
papplClientHTMLEscape(client, s, strlen(s));
break;
}
}
else
format ++;
}
if (format > start)
httpWrite2(client->http, start, (size_t)(format - start));
va_end(ap);
}
//
// 'papplClientHTMLPuts()' - Write a HTML string.
//
void
papplClientHTMLPuts(
pappl_client_t *client, // I - Client
const char *s) // I - String
{
if (client && s && *s)
httpWrite2(client->http, s, strlen(s));
}
//
// 'papplClientHTMLStartForm()' - Start a HTML form.
//
// This function starts a HTML form with the specified "action" path and
// includes the CSRF token as a hidden variable.
//
void
papplClientHTMLStartForm(
pappl_client_t *client, // I - Client
const char *action) // I - Form action URL
{
(void)client;
(void)action;
}
//
// 'papplClientHTMLValidateForm()' - Validate HTML form variables.
//
// This function validates the contents of a POST form using the CSRF token
// included as a hidden variable.
//
// Note: Callers are expected to validate all other form variables.
//
bool // O - `true` if the CSRF token is valid, `false` otherwise
papplClientHTMLValidateForm(
pappl_client_t *client, // I - Client
int num_form, // I - Number of form variables
cups_option_t *form) // I - Form variables
{
(void)client;
(void)num_form;
(void)form;
return (false);
}