]> git.ipfire.org Git - thirdparty/squid.git/blame - helpers/url_rewrite/fake/fake.cc
Boilerplate: update copyright blurbs for Squid tools
[thirdparty/squid.git] / helpers / url_rewrite / fake / fake.cc
CommitLineData
fdbb3b19 1/*
8432a09e
AJ
2 * Copyright (c) 2009-2014, Treehouse Networks Ltd. New Zealand
3 * All rights reserved.
fdbb3b19 4 *
8432a09e
AJ
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
fdbb3b19 8 *
8432a09e
AJ
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
fdbb3b19 11 *
8432a09e
AJ
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * Example URL re-writer program for Squid.
fdbb3b19 32 *
8432a09e
AJ
33 * This code gets the url and returns it. No re-writing is done.
34 * It is intended for testing use and as a base for further implementation.
fdbb3b19
AJ
35 */
36
f7f3304a 37#include "squid.h"
e673ba3a 38#include "helpers/defines.h"
fdbb3b19 39
ec85ebda 40#include <cstring>
ec85ebda 41
fdbb3b19
AJ
42/**
43 * options:
44 * -d enable debugging.
45 * -h interface help.
46 */
47char *my_program_name = NULL;
fdbb3b19
AJ
48
49static void
50usage(void)
51{
52 fprintf(stderr,
53 "Usage: %s [-d] [-v] [-h]\n"
54 " -d enable debugging.\n"
55 " -h this message\n\n",
56 my_program_name);
57}
58
59static void
60process_options(int argc, char *argv[])
61{
62 int opt, had_error = 0;
63
64 opterr = 0;
65 while (-1 != (opt = getopt(argc, argv, "hd"))) {
66 switch (opt) {
67 case 'd':
e673ba3a 68 debug_enabled = 1;
fdbb3b19
AJ
69 break;
70 case 'h':
71 usage();
72 exit(0);
73 case '?':
74 opt = optopt;
75 /* fall thru to default */
76 default:
77 fprintf(stderr, "unknown option: -%c. Exiting\n", opt);
78 usage();
79 had_error = 1;
80 }
81 }
82 if (had_error)
83 exit(1);
84}
85
fdbb3b19
AJ
86int
87main(int argc, char *argv[])
88{
e673ba3a 89 char buf[HELPER_INPUT_BUFFER];
fdbb3b19 90 int buflen = 0;
fdbb3b19
AJ
91
92 setbuf(stdout, NULL);
93 setbuf(stderr, NULL);
94
95 my_program_name = argv[0];
96
97 process_options(argc, argv);
98
c19c2c0b 99 debug("%s build " __DATE__ ", " __TIME__ " starting up...\n", my_program_name);
fdbb3b19 100
e673ba3a 101 while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != NULL) {
ec85ebda 102 char *p;
fdbb3b19
AJ
103
104 if ((p = strchr(buf, '\n')) != NULL) {
105 *p = '\0'; /* strip \n */
106 buflen = p - buf; /* length is known already */
f54f527e 107 } else
fdbb3b19
AJ
108 buflen = strlen(buf); /* keep this so we only scan the buffer for \0 once per loop */
109
c19c2c0b 110 debug("Got %d bytes '%s' from Squid\n", buflen, buf);
fdbb3b19 111
fb93aaa4
AJ
112 p = NULL;
113 int64_t channelId = strtoll(buf, &p, 10);
114 if (*p != ' ') {
115 /* send 'no-change' result back to Squid in non-concurrent format */
116 fprintf(stdout,"ERR\n");
117 } else {
118 /* send 'no-change' result back to Squid in concurrent format */
119 fprintf(stdout, "%" PRId64 " ERR\n", channelId);
120 }
fdbb3b19 121 }
c19c2c0b 122 debug("%s build " __DATE__ ", " __TIME__ " shutting down...\n", my_program_name);
fb93aaa4 123 return 0;
fdbb3b19 124}