/[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.1.1.1 by dpavlin, Wed Aug 9 06:54:43 2000 UTC revision 1.14 by dpavlin, Sun Aug 13 20:14:52 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,*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) {
61                  ap_log_reason("file permissions deny server access", r->filename, r);                  ap_log_reason("file permissions deny server access", r->filename, r);
62                  return FORBIDDEN;                      return FORBIDDEN;    
63          }          }
64          if(r->args != 0 || do_czs) {  #ifdef DEBUG
65                  ap_table_setn(r->headers_out, "X-czs_filename", r->filename);          ap_table_setn(r->headers_out, "X-czs_filename", r->filename);
66            if (r->args != 0) {
67                  ap_table_setn(r->headers_out, "X-czs_args", r->args);                  ap_table_setn(r->headers_out, "X-czs_args", r->args);
68          }          }
69            if (do_czs) ap_table_setn(r->headers_out, "X-do_czs", do_czs);
70            if (do_mac) 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
73    
74    #ifdef TEST_QUERYSTRINGV
75            if (r->args == 0 && do_czs == NULL && do_mac == NULL || ap_strcasestr(r->content_type,"cgi") != NULL) {
76    #else
77            if (do_czs == NULL && do_mac == NULL || ap_strcasestr(r->content_type,"cgi") != NULL) {
78    #endif
79                    return DECLINED;
80    /*              ap_send_fd(in, r); */
81            } else {
82    
83          r->content_type = "text/html";  /* hm? */                  regexp = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_ICASE );
84    
85          ap_soft_timeout("send", r);                  if (regexp == NULL) {
86          ap_send_http_header(r);                          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) {                  if(r->header_only) {
95                  ap_kill_timeout(r);                          ap_kill_timeout(r);
96                  ap_pfclose(r->pool, in);                          ap_pfclose(r->pool, in);
97                  return OK;                          return OK;
98          }                  }
99    
100  #ifdef TEST_QUERYSTRING  
101          if(r->args == 0 && !do_czs) {                  while(fgets(buffer,MAX_STRING_LEN,in)) {
102                  ap_send_fd(in, r);                          if (do_czs != NULL) {
103          } else {                                  for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) {
104                                            switch ( buffer[i] ) {
105                                            case '¹': buffer[i]='s'; break;
106                                            case 'ð': buffer[i]='d'; break;
107                                            case 'è': buffer[i]='c'; break;
108                                            case 'æ': buffer[i]='c'; break;
109                                            case '¾': buffer[i]='z'; break;
110                                            case '©': buffer[i]='S'; break;
111                                            case 'Ð': buffer[i]='D'; break;
112                                            case 'È': buffer[i]='C'; break;
113                                            case 'Æ': buffer[i]='C'; break;
114                                            case '®': buffer[i]='Z'; break;
115                                            }
116                                    }
117                            }
118    
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  #endif
135          while(fgets(buffer,MAX_STRING_LEN,in)) {                                  }
136                  for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) {                                  buffer[f_ch]=0;
137                          switch ( buffer[i] ) {                                  ap_rprintf(r,"%s", ap_pstrcat(r->pool, buffer,"<!-- removed charset -->", buffer+l_ch, NULL));
138                          case '¹': buffer[i]='s'; break;                          } else {
139                          case 'ð': buffer[i]='d'; break;                                  ap_rprintf(r,"%s",buffer);
                         case 'è': buffer[i]='c'; break;  
                         case 'æ': buffer[i]='c'; break;  
                         case '¾': buffer[i]='z'; break;  
                         case '©': buffer[i]='S'; break;  
                         case 'Ð': buffer[i]='D'; break;  
                         case 'È': buffer[i]='C'; break;  
                         case 'Æ': buffer[i]='C'; break;  
                         case '®': buffer[i]='Z'; break;  
140                          }                          }
141                  }                  }
142                  ap_rprintf(r,"%s",buffer);  /*              ap_pregfree(r->pool, regexp); */
         }  
 #ifdef TEST_QUERYSTRING  
143          }          }
 #endif  
144    
145          ap_kill_timeout(r);          ap_kill_timeout(r);
146          ap_pfclose(r->pool, in);          ap_pfclose(r->pool, in);
# Line 100  static int czs_handler(request_rec *r) { Line 150  static int czs_handler(request_rec *r) {
150  int translate_path(request_rec *r) {  int translate_path(request_rec *r) {
151          char *uri = r->uri;          char *uri = r->uri;
152          request_rec *subr;          request_rec *subr;
153            char *ct;
154            char *ua;
155    
156          if (uri[0]=='/' && uri[1]=='_' && uri[2]=='/') {  #ifdef DEBUG
157  #if 0          ap_table_setn(r->headers_out, "X-translate-uri", r->uri);
158                  ap_table_setn(r->headers_out, "Location", ap_pstrcat(r->pool, uri+2, "?czs", NULL));  #endif
159                  return REDIRECT;  
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  #endif
168                  subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+2, r);                  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);                  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);                  ap_destroy_sub_req(subr);
                 do_czs=1;  
196                  return OK;                  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            if (ua) ap_table_setn(r->headers_out, "X-User-Agent", ua);
234    #endif
235          return DECLINED;          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 137  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                          */
264      NULL,               /* child_exit                          */      NULL,               /* child_exit                          */
265      NULL                /* [#0] post read-request              */      NULL                /* [#0] post read-request              */
266    #ifdef EAPI
267       ,NULL,               /* EAPI: add_module                    */
268        NULL,               /* EAPI: remove_module                 */
269        NULL,               /* EAPI: rewrite_command               */
270        NULL                /* EAPI: new_connection                */
271    #endif
272  };  };
273    

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.14

  ViewVC Help
Powered by ViewVC 1.1.26