]>
Commit | Line | Data |
---|---|---|
fdbb3b19 AJ |
1 | /* |
2 | * AUTHOR: Amos Jeffries <squid3@treenet.co.nz> | |
3 | * | |
4 | * Example url re-writer program for Squid. | |
5 | * | |
6 | * This code gets the url and returns it. No re-writing is done. | |
7 | * It is intended for testing use and as a base for further implementation. | |
8 | * | |
9 | * | |
10 | * This code is copyright (C) 2009 by Treehouse Networks Ltd | |
11 | * of New Zealand. It is published and Licensed as an extension of | |
12 | * squid under the same conditions as the main squid application. | |
13 | */ | |
14 | ||
15 | #include "config.h" | |
16 | ||
ec85ebda AJ |
17 | #if HAVE_CSTRING |
18 | #include <cstring> | |
19 | #endif | |
7c572fcd FC |
20 | #if HAVE_STRING_H |
21 | #include <string.h> | |
22 | #endif | |
ec85ebda | 23 | |
fdbb3b19 AJ |
24 | #define BUFFER_SIZE 10240 |
25 | ||
26 | /** | |
27 | * options: | |
28 | * -d enable debugging. | |
29 | * -h interface help. | |
30 | */ | |
31 | char *my_program_name = NULL; | |
7c572fcd | 32 | int print_debug_messages = 0; |
fdbb3b19 AJ |
33 | |
34 | static void | |
35 | usage(void) | |
36 | { | |
37 | fprintf(stderr, | |
38 | "Usage: %s [-d] [-v] [-h]\n" | |
39 | " -d enable debugging.\n" | |
40 | " -h this message\n\n", | |
41 | my_program_name); | |
42 | } | |
43 | ||
44 | static void | |
45 | process_options(int argc, char *argv[]) | |
46 | { | |
47 | int opt, had_error = 0; | |
48 | ||
49 | opterr = 0; | |
50 | while (-1 != (opt = getopt(argc, argv, "hd"))) { | |
51 | switch (opt) { | |
52 | case 'd': | |
7c572fcd | 53 | print_debug_messages = 1; |
fdbb3b19 AJ |
54 | break; |
55 | case 'h': | |
56 | usage(); | |
57 | exit(0); | |
58 | case '?': | |
59 | opt = optopt; | |
60 | /* fall thru to default */ | |
61 | default: | |
62 | fprintf(stderr, "unknown option: -%c. Exiting\n", opt); | |
63 | usage(); | |
64 | had_error = 1; | |
65 | } | |
66 | } | |
67 | if (had_error) | |
68 | exit(1); | |
69 | } | |
70 | ||
fdbb3b19 AJ |
71 | int |
72 | main(int argc, char *argv[]) | |
73 | { | |
74 | char buf[BUFFER_SIZE]; | |
75 | int buflen = 0; | |
fdbb3b19 AJ |
76 | |
77 | setbuf(stdout, NULL); | |
78 | setbuf(stderr, NULL); | |
79 | ||
80 | my_program_name = argv[0]; | |
81 | ||
82 | process_options(argc, argv); | |
83 | ||
c19c2c0b | 84 | debug("%s build " __DATE__ ", " __TIME__ " starting up...\n", my_program_name); |
fdbb3b19 AJ |
85 | |
86 | while (fgets(buf, BUFFER_SIZE, stdin) != NULL) { | |
ec85ebda | 87 | char *p; |
fdbb3b19 AJ |
88 | |
89 | if ((p = strchr(buf, '\n')) != NULL) { | |
90 | *p = '\0'; /* strip \n */ | |
91 | buflen = p - buf; /* length is known already */ | |
f54f527e | 92 | } else |
fdbb3b19 AJ |
93 | buflen = strlen(buf); /* keep this so we only scan the buffer for \0 once per loop */ |
94 | ||
c19c2c0b | 95 | debug("Got %d bytes '%s' from Squid\n", buflen, buf); |
fdbb3b19 AJ |
96 | |
97 | /* send 'no-change' result back to Squid */ | |
98 | fprintf(stdout,"\n"); | |
99 | } | |
c19c2c0b | 100 | debug("%s build " __DATE__ ", " __TIME__ " shutting down...\n", my_program_name); |
fdbb3b19 AJ |
101 | exit(0); |
102 | } |