-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgisimple.c
2458 lines (2199 loc) · 68 KB
/
cgisimple.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
*
* Purpose : Simple CGIs to get information about Privoxy's
* status.
*
* Copyright : Written by and Copyright (C) 2001-2022 the
* Privoxy team. https://www.privoxy.org/
*
* Based on the Internet Junkbuster originally written
* by and Copyright (C) 1997 Anonymous Coders and
* Junkbusters Corporation. http://www.junkbusters.com
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* The GNU General Public License should be included with
* this file. If not, you can view it at
* http://www.gnu.org/copyleft/gpl.html
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
**********************************************************************/
#include "config.h"
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#if defined (HAVE_ACCESS) && defined (HAVE_UNISTD_H)
#include <unistd.h>
#endif /* def HAVE_ACCESS && HAVE_UNISTD_H */
#include "project.h"
#include "cgi.h"
#include "cgisimple.h"
#include "list.h"
#include "encode.h"
#include "jcc.h"
#include "filters.h"
#include "actions.h"
#include "miscutil.h"
#include "loadcfg.h"
#include "parsers.h"
#include "urlmatch.h"
#include "errlog.h"
#ifdef FEATURE_CLIENT_TAGS
#include "client-tags.h"
#endif
#ifdef FEATURE_REQUIRED_TAG
#include "requiredtag.h"
#endif
static jb_err show_defines(struct map *exports);
static jb_err cgi_show_file(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters);
static jb_err load_file(const char *filename, char **buffer, size_t *length);
/*********************************************************************
*
* Function : cgi_default
*
* Description : CGI function that is called for the CGI_SITE_1_HOST
* and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
* Boring - only exports the default exports.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : none
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory
*
*********************************************************************/
jb_err cgi_default(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
struct map *exports;
(void)parameters;
assert(csp);
assert(rsp);
if (NULL == (exports = default_exports(csp, "")))
{
return JB_ERR_MEMORY;
}
return template_fill_for_cgi(csp, "default", exports, rsp);
}
/*********************************************************************
*
* Function : cgi_error_404
*
* Description : CGI function that is called if an unknown action was
* given.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : none
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_error_404(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
struct map *exports;
assert(csp);
assert(rsp);
assert(parameters);
if (NULL == (exports = default_exports(csp, NULL)))
{
return JB_ERR_MEMORY;
}
rsp->status = strdup_or_die("404 Privoxy configuration page not found");
return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
}
#ifdef FEATURE_GRACEFUL_TERMINATION
/*********************************************************************
*
* Function : cgi_die
*
* Description : CGI function to shut down Privoxy.
* NOTE: Turning this on in a production build
* would be a BAD idea. An EXTREMELY BAD idea.
* In short, don't do it.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : none
*
* Returns : JB_ERR_OK on success
*
*********************************************************************/
jb_err cgi_die (struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
static const char status[] = "200 OK Privoxy shutdown request received";
static const char body[] =
"<html>\n"
"<head>\n"
" <title>Privoxy shutdown request received</title>\n"
" <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
" <link rel=\"stylesheet\" type=\"text/css\" href=\"" CGI_PREFIX "send-stylesheet\">\n"
"</head>\n"
"<body>\n"
"<h1>Privoxy shutdown request received</h1>\n"
"<p>Privoxy is going to shut down after the next request.</p>\n"
"</body>\n"
"</html>\n";
assert(csp);
assert(rsp);
assert(parameters);
/* quit */
g_terminate = 1;
csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
rsp->content_length = 0;
rsp->head_length = 0;
rsp->is_static = 0;
rsp->body = strdup_or_die(body);
rsp->status = strdup_or_die(status);
return JB_ERR_OK;
}
#endif /* def FEATURE_GRACEFUL_TERMINATION */
/*********************************************************************
*
* Function : cgi_show_request
*
* Description : Show the client's request and what sed() would have
* made of it.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : none
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_show_request(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
char *p;
struct map *exports;
assert(csp);
assert(rsp);
assert(parameters);
if (NULL == (exports = default_exports(csp, "show-request")))
{
return JB_ERR_MEMORY;
}
/*
* Repair the damage done to the IOB by get_header()
*/
for (p = csp->client_iob->buf; p < csp->client_iob->cur; p++)
{
if (*p == '\0') *p = '\n';
}
/*
* Export the original client's request and the one we would
* be sending to the server if this wasn't a CGI call
*/
if (map(exports, "client-request", 1, html_encode(csp->client_iob->buf), 0))
{
free_map(exports);
return JB_ERR_MEMORY;
}
if (map(exports, "processed-request", 1,
html_encode_and_free_original(
#ifdef FEATURE_HTTPS_INSPECTION
csp->http->ssl ?
list_to_text(csp->https_headers) :
#endif
list_to_text(csp->headers)
), 0))
{
free_map(exports);
return JB_ERR_MEMORY;
}
return template_fill_for_cgi(csp, "show-request", exports, rsp);
}
#ifdef FEATURE_CLIENT_TAGS
/*********************************************************************
*
* Function : cgi_create_client_tag_form
*
* Description : Creates a HTML form to enable or disable a given
* client tag.
* XXX: Could use a template.
*
* Parameters :
* 1 : form = Buffer to fill with the generated form
* 2 : size = Size of the form buffer
* 3 : tag = Name of the tag this form should affect
* 4 : toggle_state = Desired state after the button pressed 0
* 5 : expires = Whether or not the tag should be enabled.
* Only checked if toggle_state is 1.
*
* Returns : void
*
*********************************************************************/
static void cgi_create_client_tag_form(char *form, size_t size,
const char *tag, int toggle_state, int expires)
{
char *button_name;
if (toggle_state == 1)
{
button_name = (expires == 1) ? "Enable" : "Enable temporarily";
}
else
{
assert(toggle_state == 0);
button_name = "Disable";
}
snprintf(form, size,
"<form method=\"GET\" action=\""CGI_PREFIX"toggle-client-tag\" style=\"display: inline\">\n"
" <input type=\"hidden\" name=\"tag\" value=\"%s\">\n"
" <input type=\"hidden\" name=\"toggle-state\" value=\"%i\">\n"
" <input type=\"hidden\" name=\"expires\" value=\"%u\">\n"
" <input type=\"submit\" value=\"%s\">\n"
"</form>", tag, toggle_state, !expires, button_name);
}
/*********************************************************************
*
* Function : cgi_show_client_tags
*
* Description : Shows the tags that can be set based on the client
* address (opt-in).
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : none
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_show_client_tags(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
struct map *exports;
struct client_tag_spec *this_tag;
jb_err err = JB_ERR_OK;
char *client_tag_status;
char buf[1000];
time_t refresh_delay;
assert(csp);
assert(rsp);
assert(parameters);
if (NULL == (exports = default_exports(csp, "client-tags")))
{
return JB_ERR_MEMORY;
}
assert(csp->client_address != NULL);
this_tag = csp->config->client_tags;
if (this_tag->name == NULL)
{
client_tag_status = strdup_or_die("<p>No tags have been configured.</p>\n");
}
else
{
client_tag_status = strdup_or_die("<table border=\"1\">\n"
"<tr><th>Tag name</th>\n"
"<th>Current state</th><th>Change state</th><th>Description</th></tr>\n");
while ((this_tag != NULL) && (this_tag->name != NULL))
{
int tag_state;
privoxy_mutex_lock(&client_tags_mutex);
tag_state = client_has_requested_tag(csp->client_address, this_tag->name);
privoxy_mutex_unlock(&client_tags_mutex);
if (!err) err = string_append(&client_tag_status, "<tr><td>");
if (!err) err = string_append(&client_tag_status, this_tag->name);
if (!err) err = string_append(&client_tag_status, "</td><td>");
if (!err) err = string_append(&client_tag_status, tag_state == 1 ? "Enabled" : "Disabled");
if (!err) err = string_append(&client_tag_status, "</td><td>");
cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 1);
if (!err) err = string_append(&client_tag_status, buf);
if (tag_state == 0)
{
cgi_create_client_tag_form(buf, sizeof(buf), this_tag->name, !tag_state, 0);
if (!err) err = string_append(&client_tag_status, buf);
}
if (!err) err = string_append(&client_tag_status, "</td><td>");
if (!err) err = string_append(&client_tag_status, this_tag->description);
if (!err) err = string_append(&client_tag_status, "</td></tr>\n");
if (err)
{
break;
}
this_tag = this_tag->next;
}
if (!err) err = string_append(&client_tag_status, "</table>\n");
if (err)
{
free_map(exports);
return JB_ERR_MEMORY;
}
}
refresh_delay = get_next_tag_timeout_for_client(csp->client_address);
if (refresh_delay != 0)
{
snprintf(buf, sizeof(buf), "%u", csp->config->client_tag_lifetime);
if (map(exports, "refresh-delay", 1, buf, 1))
{
freez(client_tag_status);
free_map(exports);
return JB_ERR_MEMORY;
}
}
else
{
err = map_block_killer(exports, "tags-expire");
if (err != JB_ERR_OK)
{
freez(client_tag_status);
return err;
}
}
if (map(exports, "client-tags", 1, client_tag_status, 0))
{
free_map(exports);
return JB_ERR_MEMORY;
}
if (map(exports, "client-ip-addr", 1, csp->client_address, 1))
{
free_map(exports);
return JB_ERR_MEMORY;
}
return template_fill_for_cgi(csp, "client-tags", exports, rsp);
}
/*********************************************************************
*
* Function : cgi_toggle_client_tag
*
* Description : Toggles a client tag and redirects to the show-tags
* page
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : none
* 1 : tag = Name of the tag to enable or disable
* 2 : toggle-state = How to toggle the tag (0/1)
* 3 : expires = Set to 1 if the tag should be enabled
* temporarily, otherwise set to 0
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_toggle_client_tag(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
const char *toggled_tag;
const char *toggle_state;
const char *tag_expires;
time_t time_to_live;
assert(csp);
assert(rsp);
assert(parameters);
toggled_tag = lookup(parameters, "tag");
if (*toggled_tag == '\0')
{
log_error(LOG_LEVEL_ERROR, "Received tag toggle request without tag");
}
else
{
tag_expires = lookup(parameters, "expires");
if (*tag_expires == '0')
{
time_to_live = 0;
}
else
{
time_to_live = csp->config->client_tag_lifetime;
}
toggle_state = lookup(parameters, "toggle-state");
if (*toggle_state == '1')
{
enable_client_specific_tag(csp, toggled_tag, time_to_live);
}
else
{
disable_client_specific_tag(csp, toggled_tag);
}
}
rsp->status = strdup_or_die("302 Done dealing with toggle request");
if (enlist_unique_header(rsp->headers,
"Location", CGI_PREFIX "client-tags"))
{
return JB_ERR_MEMORY;
}
return JB_ERR_OK;
}
#endif /* def FEATURE_CLIENT_TAGS */
/*********************************************************************
*
* Function : cgi_send_banner
*
* Description : CGI function that returns a banner.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters :
* type : Selects the type of banner between "trans", "pattern",
* and "auto". Defaults to "pattern" if absent or invalid.
* "auto" means to select as if we were image-blocking.
* (Only the first character really counts; b and t are
* equivalent).
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_send_banner(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
char imagetype = lookup(parameters, "type")[0];
if (imagetype != 'a' && imagetype != 'b' &&
imagetype != 'p' && imagetype != 't')
{
log_error(LOG_LEVEL_ERROR, "Overruling invalid image type '%c'.",
imagetype);
imagetype = 'p';
}
/*
* If type is auto, then determine the right thing
* to do from the set-image-blocker action
*/
if (imagetype == 'a')
{
/*
* Default to pattern
*/
imagetype = 'p';
#ifdef FEATURE_IMAGE_BLOCKING
if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
{
static const char prefix1[] = CGI_PREFIX "send-banner?type=";
static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
if (p == NULL)
{
/* Use default - nothing to do here. */
}
else if (0 == strcmpic(p, "blank"))
{
imagetype = 'b';
}
else if (0 == strcmpic(p, "pattern"))
{
imagetype = 'p';
}
/*
* If the action is to call this CGI, determine
* the argument:
*/
else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
{
imagetype = p[sizeof(prefix1) - 1];
}
else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
{
imagetype = p[sizeof(prefix2) - 1];
}
/*
* Everything else must (should) be a URL to
* redirect to.
*/
else
{
imagetype = 'r';
}
}
#endif /* def FEATURE_IMAGE_BLOCKING */
}
/*
* Now imagetype is either the non-auto type we were called with,
* or it was auto and has since been determined. In any case, we
* can proceed to actually answering the request by sending a redirect
* or an image as appropriate:
*/
if (imagetype == 'r')
{
rsp->status = strdup_or_die("302 Local Redirect from Privoxy");
if (enlist_unique_header(rsp->headers, "Location",
csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
{
return JB_ERR_MEMORY;
}
}
else
{
if ((imagetype == 'b') || (imagetype == 't'))
{
rsp->body = bindup(image_blank_data, image_blank_length);
rsp->content_length = image_blank_length;
}
else
{
rsp->body = bindup(image_pattern_data, image_pattern_length);
rsp->content_length = image_pattern_length;
}
if (rsp->body == NULL)
{
return JB_ERR_MEMORY;
}
if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
{
return JB_ERR_MEMORY;
}
rsp->is_static = 1;
}
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : cgi_transparent_image
*
* Description : CGI function that sends a 1x1 transparent image.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : None
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_transparent_image(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
(void)csp;
(void)parameters;
rsp->body = bindup(image_blank_data, image_blank_length);
rsp->content_length = image_blank_length;
if (rsp->body == NULL)
{
return JB_ERR_MEMORY;
}
if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
{
return JB_ERR_MEMORY;
}
rsp->is_static = 1;
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : cgi_send_default_favicon
*
* Description : CGI function that sends the standard favicon.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : None
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_send_default_favicon(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
static const char default_favicon_data[] =
"\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
"\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
"\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
"\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
"\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
"\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
"\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
"\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
"\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
"\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
"\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
"\017\000\000";
static const size_t favicon_length = sizeof(default_favicon_data) - 1;
(void)csp;
(void)parameters;
rsp->body = bindup(default_favicon_data, favicon_length);
rsp->content_length = favicon_length;
if (rsp->body == NULL)
{
return JB_ERR_MEMORY;
}
if (enlist(rsp->headers, "Content-Type: image/x-icon"))
{
return JB_ERR_MEMORY;
}
rsp->is_static = 1;
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : cgi_send_error_favicon
*
* Description : CGI function that sends the favicon for error pages.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : None
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_send_error_favicon(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
static const char error_favicon_data[] =
"\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
"\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
"\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
"\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
"\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
"\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
"\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
"\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
"\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
"\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
"\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
"\017\000\000";
static const size_t favicon_length = sizeof(error_favicon_data) - 1;
(void)csp;
(void)parameters;
rsp->body = bindup(error_favicon_data, favicon_length);
rsp->content_length = favicon_length;
if (rsp->body == NULL)
{
return JB_ERR_MEMORY;
}
if (enlist(rsp->headers, "Content-Type: image/x-icon"))
{
return JB_ERR_MEMORY;
}
rsp->is_static = 1;
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : cgi_send_stylesheet
*
* Description : CGI function that sends a css stylesheet found
* in the cgi-style.css template
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : None
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_send_stylesheet(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
jb_err err;
assert(csp);
assert(rsp);
(void)parameters;
err = template_load(csp, &rsp->body, "cgi-style.css", 0);
if (err == JB_ERR_FILE)
{
/*
* No way to tell user; send empty stylesheet
*/
log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
}
else if (err)
{
return err; /* JB_ERR_MEMORY */
}
if (enlist(rsp->headers, "Content-Type: text/css"))
{
return JB_ERR_MEMORY;
}
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : cgi_send_wpad
*
* Description : CGI function that sends a Proxy Auto-Configuration
* (PAC) file.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : None
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_send_wpad(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
struct map *exports;
assert(csp);
assert(rsp);
assert(parameters);
if (NULL == (exports = default_exports(csp, NULL)))
{
return JB_ERR_MEMORY;
}
if (enlist(rsp->headers, "Content-Type: application/x-ns-proxy-autoconfig"))
{
free_map(exports);
return JB_ERR_MEMORY;
}
return template_fill_for_cgi(csp, "wpad.dat", exports, rsp);
}
/*********************************************************************
*
* Function : cgi_send_url_info_osd
*
* Description : CGI function that sends the OpenSearch Description
* template for the show-url-info page. It allows to
* access the page through "search engine plugins".
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : rsp = http_response data structure for output
* 3 : parameters = map of cgi parameters
*
* CGI Parameters : None
*
* Returns : JB_ERR_OK on success
* JB_ERR_MEMORY on out-of-memory error.
*
*********************************************************************/
jb_err cgi_send_url_info_osd(struct client_state *csp,
struct http_response *rsp,
const struct map *parameters)
{
jb_err err = JB_ERR_MEMORY;
struct map *exports = default_exports(csp, NULL);
(void)csp;
(void)parameters;
if (NULL != exports)
{
err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
if (JB_ERR_OK == err)
{
err = enlist(rsp->headers,
"Content-Type: application/opensearchdescription+xml");
}
}
return err;
}
/*********************************************************************
*
* Function : get_content_type
*
* Description : Use the file extension to guess the content type
* header we should use to serve the file.
*
* Parameters :
* 1 : filename = Name of the file whose content type
* we care about
*
* Returns : The guessed content type.
*
*********************************************************************/
static const char *get_content_type(const char *filename)
{
int i;
struct content_type
{
const char extension[6];
const char content_type[11];
};
static const struct content_type content_types[] =
{
{".css", "text/css"},
{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".png", "image/png"},
};
for (i = 0; i < SZ(content_types); i++)
{
if (strstr(filename, content_types[i].extension))
{
return content_types[i].content_type;
}
}