Index: trunk/debs/squid/redirector.c |
— | — | @@ -0,0 +1,130 @@ |
| 2 | +#include <stdio.h> |
| 3 | +#include<stdlib.h> |
| 4 | +#include<string.h> |
| 5 | +#include <pcre.h> |
| 6 | + |
| 7 | +#define MAX_BUFF 4096 |
| 8 | +#define OVECCOUNT 30 /* should be a multiple of 3 */ |
| 9 | + |
| 10 | +struct IN_BUFF { |
| 11 | + char *url; |
| 12 | + char *src_address; |
| 13 | + char *ident; |
| 14 | + char *method; |
| 15 | +}; |
| 16 | + |
| 17 | +int load_in_buff(char *buff, struct IN_BUFF *in_buff) { |
| 18 | + int converted; |
| 19 | + |
| 20 | + strcpy(in_buff->url, ""); |
| 21 | + strcpy(in_buff->src_address, ""); |
| 22 | + strcpy(in_buff->ident, ""); |
| 23 | + strcpy(in_buff->method, ""); |
| 24 | + |
| 25 | + converted = sscanf(buff, "%s %s %s %s\n", in_buff->url, in_buff->src_address, in_buff->ident, in_buff->method); |
| 26 | + |
| 27 | + if(converted != 4) { |
| 28 | + return 1; |
| 29 | + } |
| 30 | + |
| 31 | + if(strcmp(in_buff->src_address, "") == 0) { |
| 32 | + return 1; |
| 33 | + } |
| 34 | + |
| 35 | + if(strlen(in_buff->url) <= 4) { |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +int main(int argc, char **argv) { |
| 43 | + pcre *re; |
| 44 | + const char *error; |
| 45 | + char *pattern; |
| 46 | + unsigned char *name_table; |
| 47 | + int erroffset; |
| 48 | + int ovector[OVECCOUNT]; |
| 49 | + int subject_length; |
| 50 | + int rc, i; |
| 51 | + char buff[MAX_BUFF]; |
| 52 | + setbuf(stdout, NULL); |
| 53 | + |
| 54 | + struct IN_BUFF *in_buff = NULL; |
| 55 | + in_buff = (struct IN_BUFF *)malloc(sizeof(struct IN_BUFF)); |
| 56 | + in_buff->url = (char *)malloc(MAX_BUFF); |
| 57 | + in_buff->src_address = (char *)malloc(MAX_BUFF); |
| 58 | + in_buff->ident = (char *)malloc(MAX_BUFF); |
| 59 | + in_buff->method = (char *)malloc(MAX_BUFF); |
| 60 | + int buff_status = 0; |
| 61 | + pattern = "^http:\\/\\/(\\w+)\\.wikipedia\\.org[:\\d]*\\/(.*)"; |
| 62 | + pcre_extra *pe; |
| 63 | + |
| 64 | + char replacement_url[MAX_BUFF] = "302:http://%s.m.wikipedia.org/%s\n"; |
| 65 | + |
| 66 | + if (argv[1] != NULL) { |
| 67 | + char replacement_url_pattern[] = "302:%s\n"; |
| 68 | + char *command_line_url = argv[1]; |
| 69 | + sprintf(replacement_url, replacement_url_pattern, command_line_url); |
| 70 | + } |
| 71 | + |
| 72 | + /* make standard output line buffered */ |
| 73 | + //setvbuf(stdout, NULL, _IOLBF, 0); |
| 74 | + |
| 75 | + re = pcre_compile( |
| 76 | + pattern, /* the pattern */ |
| 77 | + 0, /* default options */ |
| 78 | + &error, /* for error message */ |
| 79 | + &erroffset, /* for error offset */ |
| 80 | + NULL); /* use default character tables */ |
| 81 | + |
| 82 | + pe = pcre_study(re, 0, &error); |
| 83 | + |
| 84 | + while(fgets(buff, MAX_BUFF, stdin) != NULL) { |
| 85 | + |
| 86 | + buff_status = load_in_buff(buff, in_buff); |
| 87 | + |
| 88 | + subject_length = (int)strlen(in_buff->url); |
| 89 | + |
| 90 | + rc = pcre_exec( |
| 91 | + re, /* the compiled pattern */ |
| 92 | + pe, /* no extra data - we didn't study the pattern */ |
| 93 | + in_buff->url, /* the subject string */ |
| 94 | + subject_length, /* the length of the subject */ |
| 95 | + 0, /* start at offset 0 in the subject */ |
| 96 | + 0, /* default options */ |
| 97 | + ovector, /* output vector for substring information */ |
| 98 | + OVECCOUNT); /* number of elements in the output vector */ |
| 99 | + |
| 100 | + if (rc < 0) { |
| 101 | + switch(rc) { |
| 102 | + case PCRE_ERROR_NOMATCH: printf("%s\n", in_buff->url); break; |
| 103 | + default: printf("Matching error %d\n", rc); break; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + char lang[20] = ""; |
| 108 | + char path[MAX_BUFF] = ""; |
| 109 | + |
| 110 | + for (i = 0; i < rc; i++) { |
| 111 | + |
| 112 | + char *substring_start = in_buff->url + ovector[2*i]; |
| 113 | + int substring_length = ovector[2*i+1] - ovector[2*i]; |
| 114 | + if (i == 1) { |
| 115 | + sprintf(lang, "%.*s", substring_length, substring_start); |
| 116 | + } else if (i == 2) { |
| 117 | + sprintf(path, "%.*s", substring_length, substring_start); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (strlen(path) > 0) { |
| 122 | + printf(replacement_url, lang, path); |
| 123 | + } |
| 124 | + |
| 125 | + fflush(stdout); |
| 126 | + } |
| 127 | + |
| 128 | + pcre_free(re); |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |