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