/[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.6 by dpavlin, Wed Aug 9 19:43:30 2000 UTC revision 1.12 by dpavlin, Sun Aug 13 18:30:24 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 32  Line 36 
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;
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;
# Line 50  static int czs_handler(request_rec *r) { Line 58  static int czs_handler(request_rec *r) {
58                  ap_log_reason("file permissions deny server access", r->filename, r);                  ap_log_reason("file permissions deny server access", r->filename, r);
59                  return FORBIDDEN;                      return FORBIDDEN;    
60          }          }
61          if(r->args != 0 || do_czs) {  #ifdef DEBUG
62            if(r->args != 0 || ap_table_get(r->notes,"do_czs")) {
63                  ap_table_setn(r->headers_out, "X-czs_filename", r->filename);                  ap_table_setn(r->headers_out, "X-czs_filename", r->filename);
64                    ap_table_setn(r->headers_out, "X-czs_content-type", r->content_type);
65                  if (r->args != 0) {                  if (r->args != 0) {
66                          ap_table_setn(r->headers_out, "X-czs_args", r->args);                          ap_table_setn(r->headers_out, "X-czs_args", r->args);
67                  }                  }
68          }          }
69    #endif
70    
71          ap_soft_timeout("send", r);  #ifdef TEST_QUERYSTRINGV
72          ap_send_http_header(r);          if(r->args == 0 && !ap_table_get(r->notes,"do_czs") ||
73                       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) {  
74  #else  #else
75          if(!do_czs) {          if(!ap_table_get(r->notes,"do_czs") ||
76                    ap_strcasestr(r->content_type,"cgi") != NULL) {
77  #endif  #endif
78                  ap_send_fd(in, r);                  return DECLINED;
79    /*              ap_send_fd(in, r); */
80          } else {          } else {
81    
82                    regexp = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_ICASE );
83    
84                    if (regexp == NULL) {
85                            ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
86                                    "unable to compile pattern \"%s\"", rexp);
87                            return DECLINED;
88                    }
89    
90                    ap_soft_timeout("send", r);
91                    ap_send_http_header(r);
92      
93                    if(r->header_only) {
94                            ap_kill_timeout(r);
95                            ap_pfclose(r->pool, in);
96                            return OK;
97                    }
98    
99    
100                  while(fgets(buffer,MAX_STRING_LEN,in)) {                  while(fgets(buffer,MAX_STRING_LEN,in)) {
101                          for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) {                          for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) {
102                                  switch ( buffer[i] ) {                                  switch ( buffer[i] ) {
# Line 89  static int czs_handler(request_rec *r) { Line 112  static int czs_handler(request_rec *r) {
112                                  case '®': buffer[i]='Z'; break;                                  case '®': buffer[i]='Z'; break;
113                                  }                                  }
114                          }                          }
115                          ap_rprintf(r,"%s",buffer);  
116    
117                            if (ap_regexec(regexp, buffer, nsub, regm, 0) == 0) {
118    #if 0
119                                    ap_rprintf(r,"<!-- num replacements: %02x buffer:\n%s\n",nsub,buffer);
120                                    ap_rprintf(r,"\n-->\n",i,regm[i].rm_so,regm[i].rm_eo);
121    #endif
122                                    for (i=0; i<10; i++) {
123                                            if (regm[i].rm_so != -1) {
124                                                    if (regm[i].rm_so < f_ch)
125                                                            f_ch = regm[i].rm_so;
126                                                    if (regm[i].rm_eo > l_ch)
127                                                            l_ch = regm[i].rm_eo;
128                                            }
129    #if 0
130                                            ap_rprintf(r,"<!-- %02x: %02x-%02x -->\n",i,regm[i].rm_so,regm[i].rm_eo);
131    #endif
132                                    }
133                                    buffer[f_ch]=0;
134                                    ap_rprintf(r,"%s", ap_pstrcat(r->pool, buffer,"<!-- removed charset -->", buffer+l_ch, NULL));
135                            } else {
136                                    ap_rprintf(r,"%s",buffer);
137                            }
138                  }                  }
139    /*              ap_pregfree(r->pool, regexp); */
140          }          }
141    
142          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  int translate_path(request_rec *r) {  int translate_path(request_rec *r) {
148          char *uri = r->uri;          char *uri = r->uri;
149          request_rec *subr;          request_rec *subr;
150          do_czs=0;          char *type = NULL;
151    
152          if (uri[0]=='/' && uri[1]=='c' && uri[2]=='z' && uri[3]=='s') {  #ifdef DEBUG
153            ap_table_setn(r->headers_out, "X-translate", r->uri);
154    #endif
155            if (uri[0]=='/' && (uri[1] | 0x20) =='c' && (uri[2] | 0x20)=='z' && (uri[3] | 0x20) =='s') {
156  #if 0  #if 0
157                  ap_table_setn(r->headers_out, "Location", ap_pstrcat(r->pool, uri+2, "?czs", NULL));                  ap_table_setn(r->headers_out, "Location", ap_pstrcat(r->pool, uri+2, "?czs", NULL));
158                  return REDIRECT;                  return REDIRECT;
159  #endif  #endif
160                  subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+4, r);                  subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+4, r);
161                  r->filename=ap_pstrdup(r->pool, subr->filename);                  r->filename=ap_pstrdup(r->pool, subr->filename);
162                    type = subr->content_type;
163    #ifdef DEBUG
164                    ap_table_setn(r->headers_out, "X-translate-content-type", type);
165    #endif
166                    if (type == NULL) {
167                            return DECLINED; /* hm? */
168                    }
169                    ap_table_setn(r->notes,"do_czs",1);
170                  ap_destroy_sub_req(subr);                  ap_destroy_sub_req(subr);
                 do_czs=1;  
171                  return OK;                  return OK;
172          }          }
173    
174          return DECLINED;          return DECLINED;
175  }  }
176        
177    static int add_charset_header(request_rec * r)
178    {
179            const char *ua, *ct;
180    
181            ua = ap_table_get(r->headers_in, "User-Agent");
182            if (ua == NULL)
183                    ua = "<unknown>";
184    
185            ct = r->content_type;
186    
187            if (ct != NULL && !ap_table_get(r->notes,"do_czs")) {
188    
189                    if (strstr(ct, "text/html") == NULL)
190                            return DECLINED; /* Don't mess with other types */
191    
192                    if (strstr(ua, "Mac") != NULL) {
193                            if (strstr(ua, "MSIE") != NULL)
194                                    r->content_type = "text/html; charset=x-mac-roman";
195                            else
196                                    r->content_type = "text/html; charset=MacCE";
197                    } else
198                            r->content_type = "text/html; charset=iso-8859-2";
199            }
200    
201    #ifdef DEBUG
202            ap_table_setn(r->headers_out, "X-Content-Type", ct);
203            ap_table_setn(r->headers_out, "X-new-Content-Type", r->content_type);
204            ap_table_setn(r->headers_out, "X-User-Agent", ua);
205    #endif
206            return DECLINED;
207    }
208    
209    
210  /* Dispatch list of content handlers */  /* Dispatch list of content handlers */
211  static const handler_rec czs_handlers[] = {  static const handler_rec czs_handlers[] = {
212    { "czs", czs_handler },    { "czs", czs_handler },
# Line 139  module MODULE_VAR_EXPORT czs_module = { Line 228  module MODULE_VAR_EXPORT czs_module = {
228      NULL,               /* [#5] check if the user is ok _here_ */      NULL,               /* [#5] check if the user is ok _here_ */
229      NULL,               /* [#2] check access by host address   */      NULL,               /* [#2] check access by host address   */
230      NULL,               /* [#6] determine MIME type            */      NULL,               /* [#6] determine MIME type            */
231      NULL,               /* [#7] pre-run fixups                 */      add_charset_header, /* [#7] pre-run fixups                 */
232      NULL,               /* [#9] log a transaction              */      NULL,               /* [#9] log a transaction              */
233      NULL,               /* [#3] header parser                  */      NULL,               /* [#3] header parser                  */
234      NULL,               /* child_init                          */      NULL,               /* child_init                          */
235      NULL,               /* child_exit                          */      NULL,               /* child_exit                          */
236      NULL                /* [#0] post read-request              */      NULL                /* [#0] post read-request              */
237    #ifdef EAPI
238       ,NULL,               /* EAPI: add_module                    */
239        NULL,               /* EAPI: remove_module                 */
240        NULL,               /* EAPI: rewrite_command               */
241        NULL                /* EAPI: new_connection                */
242    #endif
243  };  };
244    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.12

  ViewVC Help
Powered by ViewVC 1.1.26