/[mod_czs]/mod_czs.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /mod_czs.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by dpavlin, Wed Aug 9 21:09:50 2000 UTC revision 1.13 by dpavlin, Sun Aug 13 20:11:51 2000 UTC
# Line 1  Line 1 
1  /*  /*
2   * mod_czs -- nuke iso8859-2 characters   * mod_czs -- nuke iso8859-2 characters
3     * sets `Contnet-type: text/html; charset=foobar' based on user agent string
4     * from client browser
5     *
6   * Dobrica Pavlinusic <dpavlin@rot13.org>   * Dobrica Pavlinusic <dpavlin@rot13.org>
7     * Robert Avilov <ravilov@linux.hr> (some fixes of ua_charset part)
8   *   *
9   * based on mod_format by Stephen F. Booth and   * based on mod_format by Stephen F. Booth and
10   * mod_mocrify by Peter Triller, Ernst Bachmann   * mod_mocrify by Peter Triller, Ernst Bachmann
# Line 28  Line 32 
32  #include "http_config.h"  #include "http_config.h"
33  #include "http_log.h"  #include "http_log.h"
34  #include "http_protocol.h"  #include "http_protocol.h"
 #include "translate.c"  
35    
36  module MODULE_VAR_EXPORT mod_czs;  module MODULE_VAR_EXPORT mod_czs;
37    
38  FILE *in;  FILE *in;
 int do_czs;  
39    
40  static int czs_handler(request_rec *r) {  static int czs_handler(request_rec *r) {
41    
42          char buffer[MAX_STRING_LEN] = "undefined buffer content";          char buffer[MAX_STRING_LEN] = "undefined buffer content";
43          int i;          char *b,*do_czs,*do_mac;
44            char *rexp = "< *META *HTTP-EQUIV=\"(Content-Type)\" *CONTENT=\"(text/html[^\"]+)\" *>";
45            regex_t *regexp;
46            int nsub = 10;
47            regmatch_t regm[10];
48            int i,f_ch=MAX_STRING_LEN,l_ch=0;
49    
50          if(r->method_number != M_GET)          if(r->method_number != M_GET)
51                  return DECLINED;                  return DECLINED;
52          if(r->finfo.st_mode == 0)          if(r->finfo.st_mode == 0)
53                  return NOT_FOUND;                  return NOT_FOUND;
54    
55            do_czs = (char *)ap_table_get(r->notes,"do_czs");
56            do_mac = (char *)ap_table_get(r->notes,"do_mac");
57    
58          in = ap_pfopen(r->pool, r->filename, "r");          in = ap_pfopen(r->pool, r->filename, "r");
59    
60          if(in == NULL) {          if(in == NULL) {
# Line 52  static int czs_handler(request_rec *r) { Line 62  static int czs_handler(request_rec *r) {
62                  return FORBIDDEN;                      return FORBIDDEN;    
63          }          }
64  #ifdef DEBUG  #ifdef DEBUG
65          if(r->args != 0 || do_czs) {          ap_table_setn(r->headers_out, "X-czs_filename", r->filename);
66                  ap_table_setn(r->headers_out, "X-czs_filename", r->filename);          if (r->args != 0) {
67                  if (r->args != 0) {                  ap_table_setn(r->headers_out, "X-czs_args", r->args);
                         ap_table_setn(r->headers_out, "X-czs_args", r->args);  
                 }  
68          }          }
69            ap_table_setn(r->headers_out, "X-do_czs", do_czs);
70            ap_table_setn(r->headers_out, "X-do_mac", do_mac);
71            ap_table_setn(r->headers_out, "X-content-type", r->content_type);
72  #endif  #endif
73    
74          ap_soft_timeout("send", r);  #ifdef TEST_QUERYSTRINGV
75          ap_send_http_header(r);          if (r->args == 0 && do_czs == NULL && do_mac == NULL || ap_strcasestr(r->content_type,"cgi") != NULL) {
     
         if(r->header_only) {  
                 ap_kill_timeout(r);  
                 ap_pfclose(r->pool, in);  
                 return OK;  
         }  
   
 #ifdef TEST_QUERYSTRING  
         if(r->args == 0 && !do_czs) {  
76  #else  #else
77          if(!do_czs) {          if (do_czs == NULL && do_mac == NULL || ap_strcasestr(r->content_type,"cgi") != NULL) {
78  #endif  #endif
79                  ap_send_fd(in, r);                  return DECLINED;
80    /*              ap_send_fd(in, r); */
81          } else {          } else {
82    
83                    regexp = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_ICASE );
84    
85                    if (regexp == NULL) {
86                            ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
87                                    "unable to compile pattern \"%s\"", rexp);
88                            return DECLINED;
89                    }
90    
91                    ap_soft_timeout("send", r);
92                    ap_send_http_header(r);
93      
94                    if(r->header_only) {
95                            ap_kill_timeout(r);
96                            ap_pfclose(r->pool, in);
97                            return OK;
98                    }
99    
100    
101                  while(fgets(buffer,MAX_STRING_LEN,in)) {                  while(fgets(buffer,MAX_STRING_LEN,in)) {
102                          for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) {                          if (do_czs != NULL) {
103                                  switch ( buffer[i] ) {                                  for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) {
104                                  case '¹': buffer[i]='s'; break;                                          switch ( buffer[i] ) {
105                                  case 'ð': buffer[i]='d'; break;                                          case '¹': buffer[i]='s'; break;
106                                  case 'è': buffer[i]='c'; break;                                          case 'ð': buffer[i]='d'; break;
107                                  case 'æ': buffer[i]='c'; break;                                          case 'è': buffer[i]='c'; break;
108                                  case '¾': buffer[i]='z'; break;                                          case 'æ': buffer[i]='c'; break;
109                                  case '©': buffer[i]='S'; break;                                          case '¾': buffer[i]='z'; break;
110                                  case 'Ð': buffer[i]='D'; break;                                          case '©': buffer[i]='S'; break;
111                                  case 'È': buffer[i]='C'; break;                                          case 'Ð': buffer[i]='D'; break;
112                                  case 'Æ': buffer[i]='C'; break;                                          case 'È': buffer[i]='C'; break;
113                                  case '®': buffer[i]='Z'; break;                                          case 'Æ': buffer[i]='C'; break;
114                                            case '®': buffer[i]='Z'; break;
115                                            }
116                                  }                                  }
117                          }                          }
118                          ap_rprintf(r,"%s",buffer);  
119    
120                            if (ap_regexec(regexp, buffer, nsub, regm, 0) == 0) {
121    #if 0
122                                    ap_rprintf(r,"<!-- num replacements: %02x buffer:\n%s\n",nsub,buffer);
123                                    ap_rprintf(r,"\n-->\n",i,regm[i].rm_so,regm[i].rm_eo);
124    #endif
125                                    for (i=0; i<10; i++) {
126                                            if (regm[i].rm_so != -1) {
127                                                    if (regm[i].rm_so < f_ch)
128                                                            f_ch = regm[i].rm_so;
129                                                    if (regm[i].rm_eo > l_ch)
130                                                            l_ch = regm[i].rm_eo;
131                                            }
132    #if 0
133                                            ap_rprintf(r,"<!-- %02x: %02x-%02x -->\n",i,regm[i].rm_so,regm[i].rm_eo);
134    #endif
135                                    }
136                                    buffer[f_ch]=0;
137                                    ap_rprintf(r,"%s", ap_pstrcat(r->pool, buffer,"<!-- removed charset -->", buffer+l_ch, NULL));
138                            } else {
139                                    ap_rprintf(r,"%s",buffer);
140                            }
141                  }                  }
142    /*              ap_pregfree(r->pool, regexp); */
143          }          }
144    
145          ap_kill_timeout(r);          ap_kill_timeout(r);
# Line 101  static int czs_handler(request_rec *r) { Line 147  static int czs_handler(request_rec *r) {
147          return OK;          return OK;
148  }  }
149    
150    int translate_path(request_rec *r) {
151            char *uri = r->uri;
152            request_rec *subr;
153            char *ct;
154            char *ua;
155    
156    #ifdef DEBUG
157            ap_table_setn(r->headers_out, "X-translate-uri", r->uri);
158    #endif
159    
160            if (uri[0]=='/' && (uri[1] | 0x20) =='c' && (uri[2] | 0x20)=='z' && (uri[3] | 0x20) =='s' && uri[4]=='/') {
161                    subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+4, r);
162                    r->filename=ap_pstrdup(r->pool, subr->filename);
163                    ct = (char *)subr->content_type;
164                    if (ct == NULL) return DECLINED; /* hm? */
165    #ifdef DEBUG
166                    ap_table_setn(r->headers_out, "X-translate-sub-ct", ct);
167    #endif
168                    ap_table_setn(r->notes,"do_czs",ap_pstrdup(r->pool,"1"));
169                    ap_destroy_sub_req(subr);
170                    return OK;
171            }
172    
173            ua = (char *)ap_table_get(r->headers_in, "User-Agent");
174            if (ua == NULL) ua = "unknown";
175    
176    #ifdef DEBUG
177            ap_table_setn(r->headers_out, "X-translate-UA", ua);
178    #endif
179    
180            if (uri[0]=='/' && (uri[1] | 0x20) =='m' && (uri[2] | 0x20)=='a' && (uri[3] | 0x20) =='c' && uri[4]=='/') {
181                    subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+4, r);
182                    r->filename=ap_pstrdup(r->pool, subr->filename);
183                    ct = (char *)subr->content_type;
184                    if (ct == NULL) return DECLINED; /* hm? */
185    #ifdef DEBUG
186                    ap_table_setn(r->headers_out, "X-translate-sub-ct", ct);
187    #endif
188                    /* will store User-Agent in do_mac as true value */
189                    if (ap_strcasestr(ua, "Mac"))
190                            ap_table_setn(r->notes,"do_mac",ua);
191                    else {
192                            ap_table_setn(r->headers_out, "Location", r->uri+4);
193                            return REDIRECT;
194                    }
195                    ap_destroy_sub_req(subr);
196                    return OK;
197            }
198    
199    #if 0
200            if (ap_strcasestr(ua, "Mac")) {
201                    ap_table_setn(r->headers_out, "Location", ap_pstrcat(r->pool, "/mac/", r->uri, NULL));
202                    return REDIRECT;
203            }
204    #endif
205    
206            return DECLINED;
207    }
208    
209    static int add_charset_header(request_rec * r)
210    {
211            char *ua, *ct;
212    
213            ua = (char *)ap_table_get(r->notes, "do_mac");
214            ct = (char *)r->content_type;
215    
216            if (ct != NULL && !ap_table_get(r->notes,"do_czs")) {
217    
218                    if (ap_strcasestr(ct, "text") == NULL)
219                            return DECLINED; /* Don't mess with other types */
220    
221                    if (ua) {       /* do_mac */
222                            if (ap_strcasestr(ua, "MSIE") != NULL)
223                                    r->content_type = ap_pstrcat(r->pool, ct,"; charset=x-mac-roman", NULL);
224                            else
225                                    r->content_type = ap_pstrcat(r->pool, ct,"; charset=MacCE", NULL);
226                    } else
227                            r->content_type = ap_pstrcat(r->pool, ct,"; charset=iso-8859-2", NULL);
228            }
229    
230    #ifdef DEBUG
231            ap_table_setn(r->headers_out, "X-Content-Type", ct);
232            ap_table_setn(r->headers_out, "X-new-Content-Type", r->content_type);
233            ap_table_setn(r->headers_out, "X-User-Agent", ua);
234    #endif
235            return DECLINED;
236    }
237    
238    
239  /* Dispatch list of content handlers */  /* Dispatch list of content handlers */
240  static const handler_rec czs_handlers[] = {  static const handler_rec czs_handlers[] = {
241    { "czs", czs_handler },    { "czs", czs_handler },
# Line 122  module MODULE_VAR_EXPORT czs_module = { Line 257  module MODULE_VAR_EXPORT czs_module = {
257      NULL,               /* [#5] check if the user is ok _here_ */      NULL,               /* [#5] check if the user is ok _here_ */
258      NULL,               /* [#2] check access by host address   */      NULL,               /* [#2] check access by host address   */
259      NULL,               /* [#6] determine MIME type            */      NULL,               /* [#6] determine MIME type            */
260      NULL,               /* [#7] pre-run fixups                 */      add_charset_header, /* [#7] pre-run fixups                 */
261      NULL,               /* [#9] log a transaction              */      NULL,               /* [#9] log a transaction              */
262      NULL,               /* [#3] header parser                  */      NULL,               /* [#3] header parser                  */
263      NULL,               /* child_init                          */      NULL,               /* child_init                          */

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.13

  ViewVC Help
Powered by ViewVC 1.1.26