-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mod_rewrite.c
4736 lines (4195 loc) · 149 KB
/
mod_rewrite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* _ _ _
** _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
** | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
** | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
** |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
** |_____|
**
** URL Rewriting Module
**
** This module uses a rule-based rewriting engine (based on a
** regular-expression parser) to rewrite requested URLs on the fly.
**
** It supports an unlimited number of additional rule conditions (which can
** operate on a lot of variables, even on HTTP headers) for granular
** matching and even external database lookups (either via plain text
** tables, DBM hash files or even external processes) for advanced URL
** substitution.
**
** It operates on the full URLs (including the PATH_INFO part) both in
** per-server context (httpd.conf) and per-dir context (.htaccess) and even
** can generate QUERY_STRING parts on result. The rewriting result finally
** can lead to internal subprocessing, external request redirection or even
** to internal proxy throughput.
**
** This module was originally written in April 1996 and
** gifted exclusively to the The Apache Software Foundation in July 1997 by
**
** Ralf S. Engelschall
** rse engelschall.com
** www.engelschall.com
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_hash.h"
#include "apr_user.h"
#include "apr_lib.h"
#include "apr_signal.h"
#include "apr_global_mutex.h"
#define APR_WANT_STRFUNC
#define APR_WANT_IOVEC
#include "apr_want.h"
#if APR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#if APR_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "mod_rewrite.h"
/* mod_ssl.h is not safe for inclusion in 2.0, so duplicate the
* optional function declarations. */
APR_DECLARE_OPTIONAL_FN(char *, ssl_var_lookup,
(apr_pool_t *, server_rec *,
conn_rec *, request_rec *,
char *));
APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));
#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
#include "unixd.h"
#define MOD_REWRITE_SET_MUTEX_PERMS /* XXX Apache should define something */
#endif
/*
** +-------------------------------------------------------+
** | |
** | static module configuration
** | |
** +-------------------------------------------------------+
*/
/*
** Our interface to the Apache server kernel:
**
** o Runtime logic of a request is as following:
** while(request or subrequest)
** foreach(stage #0...#9)
** foreach(module) (**)
** try to run hook
**
** o the order of modules at (**) is the inverted order as
** given in the "Configuration" file, i.e. the last module
** specified is the first one called for each hook!
** The core module is always the last!
**
** o there are two different types of result checking and
** continue processing:
** for hook #0,#1,#4,#5,#6,#8:
** hook run loop stops on first modules which gives
** back a result != DECLINED, i.e. it usually returns OK
** which says "OK, module has handled this _stage_" and for #1
** this have not to mean "Ok, the filename is now valid".
** for hook #2,#3,#7,#9:
** all hooks are run, independend of result
**
** o at the last stage, the core module always
** - says "HTTP_BAD_REQUEST" if r->filename does not begin with "/"
** - prefix URL with document_root or replaced server_root
** with document_root and sets r->filename
** - always return a "OK" independed if the file really exists
** or not!
*/
/* the module (predeclaration) */
module AP_MODULE_DECLARE_DATA rewrite_module;
/* rewritemap int: handler function registry */
static apr_hash_t *mapfunc_hash;
/* the cache */
static cache *cachep;
/* whether proxy module is available or not */
static int proxy_available;
static const char *lockname;
static apr_global_mutex_t *rewrite_mapr_lock_acquire = NULL;
static apr_global_mutex_t *rewrite_log_lock = NULL;
/* Optional functions imported from mod_ssl when loaded: */
static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *rewrite_ssl_lookup = NULL;
static APR_OPTIONAL_FN_TYPE(ssl_is_https) *rewrite_is_https = NULL;
/*
** +-------------------------------------------------------+
** | |
** | configuration directive handling
** | |
** +-------------------------------------------------------+
*/
/*
**
** per-server configuration structure handling
**
*/
static void *config_server_create(apr_pool_t *p, server_rec *s)
{
rewrite_server_conf *a;
a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
a->state = ENGINE_DISABLED;
a->options = OPTION_NONE;
a->rewritelogfile = NULL;
a->rewritelogfp = NULL;
a->rewriteloglevel = 0;
a->rewritemaps = apr_array_make(p, 2, sizeof(rewritemap_entry));
a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
a->server = s;
a->redirect_limit = 0; /* unset (use default) */
return (void *)a;
}
static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
{
rewrite_server_conf *a, *base, *overrides;
a = (rewrite_server_conf *)apr_pcalloc(p,
sizeof(rewrite_server_conf));
base = (rewrite_server_conf *)basev;
overrides = (rewrite_server_conf *)overridesv;
a->state = (overrides->state_set == 0) ? base->state : overrides->state;
a->state_set = overrides->state_set || base->state_set;
a->options = (overrides->options_set == 0) ? base->options : overrides->options;
a->options_set = overrides->options_set || base->options_set;
a->server = overrides->server;
a->redirect_limit = overrides->redirect_limit
? overrides->redirect_limit
: base->redirect_limit;
if (a->options & OPTION_INHERIT) {
/*
* local directives override
* and anything else is inherited
*/
a->rewriteloglevel = overrides->rewriteloglevel != 0
? overrides->rewriteloglevel
: base->rewriteloglevel;
a->rewritelogfile = overrides->rewritelogfile != NULL
? overrides->rewritelogfile
: base->rewritelogfile;
a->rewritelogfp = overrides->rewritelogfp != NULL
? overrides->rewritelogfp
: base->rewritelogfp;
a->rewritemaps = apr_array_append(p, overrides->rewritemaps,
base->rewritemaps);
a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
base->rewriteconds);
a->rewriterules = apr_array_append(p, overrides->rewriterules,
base->rewriterules);
}
else {
/*
* local directives override
* and anything else gets defaults
*/
a->rewriteloglevel = overrides->rewriteloglevel;
a->rewritelogfile = overrides->rewritelogfile;
a->rewritelogfp = overrides->rewritelogfp;
a->rewritemaps = overrides->rewritemaps;
a->rewriteconds = overrides->rewriteconds;
a->rewriterules = overrides->rewriterules;
}
return (void *)a;
}
/*
**
** per-directory configuration structure handling
**
*/
static void *config_perdir_create(apr_pool_t *p, char *path)
{
rewrite_perdir_conf *a;
a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf));
a->state = ENGINE_DISABLED;
a->options = OPTION_NONE;
a->baseurl = NULL;
a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
a->redirect_limit = 0; /* unset (use server config) */
if (path == NULL) {
a->directory = NULL;
}
else {
/* make sure it has a trailing slash */
if (path[strlen(path)-1] == '/') {
a->directory = apr_pstrdup(p, path);
}
else {
a->directory = apr_pstrcat(p, path, "/", NULL);
}
}
return (void *)a;
}
static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv)
{
rewrite_perdir_conf *a, *base, *overrides;
a = (rewrite_perdir_conf *)apr_pcalloc(p,
sizeof(rewrite_perdir_conf));
base = (rewrite_perdir_conf *)basev;
overrides = (rewrite_perdir_conf *)overridesv;
a->state = (overrides->state_set == 0) ? base->state : overrides->state;
a->state_set = overrides->state_set || base->state_set;
a->options = (overrides->options_set == 0) ? base->options : overrides->options;
a->options_set = overrides->options_set || base->options_set;
a->redirect_limit = overrides->redirect_limit
? overrides->redirect_limit
: base->redirect_limit;
if (a->options & OPTION_MERGEBASE) {
a->baseurl = (overrides->baseurl_set == 0) ? base->baseurl : overrides->baseurl;
a->baseurl_set = overrides->baseurl_set || base->baseurl_set;
}
else {
a->baseurl = overrides->baseurl;
}
a->directory = overrides->directory;
if (a->options & OPTION_INHERIT) {
a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
base->rewriteconds);
a->rewriterules = apr_array_append(p, overrides->rewriterules,
base->rewriterules);
}
else {
a->rewriteconds = overrides->rewriteconds;
a->rewriterules = overrides->rewriterules;
}
return (void *)a;
}
/*
**
** the configuration commands
**
*/
static const char *cmd_rewriteengine(cmd_parms *cmd,
void *in_dconf, int flag)
{
rewrite_perdir_conf *dconf = in_dconf;
rewrite_server_conf *sconf;
sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
/* server command? set both global scope and base directory scope */
if (cmd->path == NULL) {
sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
sconf->state_set = 1;
dconf->state = sconf->state;
dconf->state_set = 1;
}
/* directory command? set directory scope only */
else {
dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
dconf->state_set = 1;
}
return NULL;
}
static const char *cmd_rewriteoptions(cmd_parms *cmd,
void *in_dconf, const char *option)
{
int options = 0, limit = 0;
char *w;
while (*option) {
w = ap_getword_conf(cmd->pool, &option);
if (!strcasecmp(w, "inherit")) {
options |= OPTION_INHERIT;
}
else if (!strncasecmp(w, "MaxRedirects=", 13)) {
limit = atoi(&w[13]);
if (limit <= 0) {
return "RewriteOptions: MaxRedirects takes a number greater "
"than zero.";
}
}
else if (!strcasecmp(w, "MaxRedirects")) { /* be nice */
return "RewriteOptions: MaxRedirects has the format MaxRedirects"
"=n.";
}
else if (!strcasecmp(w, "allowanyuri")) {
options |= OPTION_ANYURI;
}
else if (!strcasecmp(w, "mergebase")) {
options |= OPTION_MERGEBASE;
}
else {
return apr_pstrcat(cmd->pool, "RewriteOptions: unknown option '",
w, "'", NULL);
}
}
/* server command? set both global scope and base directory scope */
if (cmd->path == NULL) { /* is server command */
rewrite_perdir_conf *dconf = in_dconf;
rewrite_server_conf *sconf =
ap_get_module_config(cmd->server->module_config,
&rewrite_module);
sconf->options |= options;
sconf->options_set = 1;
sconf->redirect_limit = limit;
dconf->options |= options;
dconf->options_set = 1;
dconf->redirect_limit = limit;
}
/* directory command? set directory scope only */
else { /* is per-directory command */
rewrite_perdir_conf *dconf = in_dconf;
dconf->options |= options;
dconf->options_set = 1;
dconf->redirect_limit = limit;
}
return NULL;
}
static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1)
{
rewrite_server_conf *sconf;
sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
sconf->rewritelogfile = a1;
return NULL;
}
static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf,
const char *a1)
{
rewrite_server_conf *sconf;
sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
sconf->rewriteloglevel = atoi(a1);
return NULL;
}
static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
const char *a2)
{
rewrite_server_conf *sconf;
rewritemap_entry *newmap;
apr_finfo_t st;
sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
newmap = apr_array_push(sconf->rewritemaps);
newmap->name = a1;
newmap->func = NULL;
if (strncmp(a2, "txt:", 4) == 0) {
newmap->type = MAPTYPE_TXT;
newmap->datafile = a2+4;
newmap->checkfile = a2+4;
newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
(void *)cmd->server, a1);
}
else if (strncmp(a2, "rnd:", 4) == 0) {
newmap->type = MAPTYPE_RND;
newmap->datafile = a2+4;
newmap->checkfile = a2+4;
newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
(void *)cmd->server, a1);
}
else if (strncmp(a2, "dbm", 3) == 0) {
const char *ignored_fname;
int bad = 0;
apr_status_t rv;
newmap->type = MAPTYPE_DBM;
newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
(void *)cmd->server, a1);
if (a2[3] == ':') {
newmap->dbmtype = "default";
newmap->datafile = a2+4;
}
else if (a2[3] == '=') {
const char *colon = ap_strchr_c(a2 + 4, ':');
if (colon) {
newmap->dbmtype = apr_pstrndup(cmd->pool, a2 + 4,
colon - (a2 + 3) - 1);
newmap->datafile = colon + 1;
}
else {
++bad;
}
}
else {
++bad;
}
if (bad) {
return apr_pstrcat(cmd->pool, "RewriteMap: bad map:",
a2, NULL);
}
rv = apr_dbm_get_usednames_ex(cmd->pool, newmap->dbmtype,
newmap->datafile, &newmap->checkfile,
&ignored_fname);
if (rv != APR_SUCCESS) {
return apr_pstrcat(cmd->pool, "RewriteMap: dbm type ",
newmap->dbmtype, " is invalid", NULL);
}
}
else if (strncmp(a2, "prg:", 4) == 0) {
newmap->type = MAPTYPE_PRG;
apr_tokenize_to_argv(a2 + 4, &newmap->argv, cmd->pool);
newmap->datafile = NULL;
newmap->checkfile = newmap->argv[0];
newmap->cachename = NULL;
}
else if (strncmp(a2, "int:", 4) == 0) {
newmap->type = MAPTYPE_INT;
newmap->datafile = NULL;
newmap->checkfile = NULL;
newmap->cachename = NULL;
newmap->func = (char *(*)(request_rec *,char *))
apr_hash_get(mapfunc_hash, a2+4, strlen(a2+4));
if ((sconf->state == ENGINE_ENABLED) && (newmap->func == NULL)) {
return apr_pstrcat(cmd->pool, "RewriteMap: internal map not found:",
a2+4, NULL);
}
}
else {
newmap->type = MAPTYPE_TXT;
newmap->datafile = a2;
newmap->checkfile = a2;
newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
(void *)cmd->server, a1);
}
newmap->fpin = NULL;
newmap->fpout = NULL;
if (newmap->checkfile && (sconf->state == ENGINE_ENABLED)
&& (apr_stat(&st, newmap->checkfile, APR_FINFO_MIN,
cmd->pool) != APR_SUCCESS)) {
return apr_pstrcat(cmd->pool,
"RewriteMap: file for map ", newmap->name,
" not found:", newmap->checkfile, NULL);
}
return NULL;
}
static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, const char *a1)
{
const char *error;
if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL)
return error;
/* fixup the path, especially for rewritelock_remove() */
lockname = ap_server_root_relative(cmd->pool, a1);
if (!lockname) {
return apr_pstrcat(cmd->pool, "Invalid RewriteLock path ", a1);
}
return NULL;
}
static const char *cmd_rewritebase(cmd_parms *cmd, void *in_dconf,
const char *a1)
{
rewrite_perdir_conf *dconf = in_dconf;
if (cmd->path == NULL || dconf == NULL) {
return "RewriteBase: only valid in per-directory config files";
}
if (a1[0] == '\0') {
return "RewriteBase: empty URL not allowed";
}
if (a1[0] != '/') {
return "RewriteBase: argument is not a valid URL";
}
dconf->baseurl = a1;
dconf->baseurl_set = 1;
return NULL;
}
static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
const char *in_str)
{
rewrite_perdir_conf *dconf = in_dconf;
char *str = apr_pstrdup(cmd->pool, in_str);
rewrite_server_conf *sconf;
rewritecond_entry *newcond;
regex_t *regexp;
char *a1;
char *a2;
char *a3;
char *cp;
const char *err;
int rc;
sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
/* make a new entry in the internal temporary rewrite rule list */
if (cmd->path == NULL) { /* is server command */
newcond = apr_array_push(sconf->rewriteconds);
}
else { /* is per-directory command */
newcond = apr_array_push(dconf->rewriteconds);
}
/* parse the argument line ourself */
if (parseargline(str, &a1, &a2, &a3)) {
return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
"'", NULL);
}
/* arg1: the input string */
newcond->input = apr_pstrdup(cmd->pool, a1);
/* arg3: optional flags field
(this have to be first parsed, because we need to
know if the regex should be compiled with ICASE!) */
newcond->flags = CONDFLAG_NONE;
if (a3 != NULL) {
if ((err = cmd_rewritecond_parseflagfield(cmd->pool, newcond,
a3)) != NULL) {
return err;
}
}
/* arg2: the pattern
try to compile the regexp to test if is ok */
cp = a2;
if (cp[0] == '!') {
newcond->flags |= CONDFLAG_NOTMATCH;
cp++;
}
/* now be careful: Under the POSIX regex library
we can compile the pattern for case insensitive matching,
under the old V8 library we have to do it self via a hack */
if (newcond->flags & CONDFLAG_NOCASE) {
rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED|REG_ICASE))
== NULL);
}
else {
rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED)) == NULL);
}
if (rc) {
return apr_pstrcat(cmd->pool,
"RewriteCond: cannot compile regular expression '",
a2, "'", NULL);
}
newcond->pattern = apr_pstrdup(cmd->pool, cp);
newcond->regexp = regexp;
return NULL;
}
static const char *cmd_rewritecond_parseflagfield(apr_pool_t *p,
rewritecond_entry *cfg,
char *str)
{
char *cp;
char *cp1;
char *cp2;
char *cp3;
char *key;
char *val;
const char *err;
if (str[0] != '[' || str[strlen(str)-1] != ']') {
return "RewriteCond: bad flag delimiters";
}
cp = str+1;
str[strlen(str)-1] = ','; /* for simpler parsing */
for ( ; *cp != '\0'; ) {
/* skip whitespaces */
for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
;
if (*cp == '\0') {
break;
}
cp1 = cp;
if ((cp2 = strchr(cp, ',')) != NULL) {
cp = cp2+1;
for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
;
*cp2 = '\0';
if ((cp3 = strchr(cp1, '=')) != NULL) {
*cp3 = '\0';
key = cp1;
val = cp3+1;
}
else {
key = cp1;
val = "";
}
if ((err = cmd_rewritecond_setflag(p, cfg, key, val)) != NULL) {
return err;
}
}
else {
break;
}
}
return NULL;
}
static const char *cmd_rewritecond_setflag(apr_pool_t *p,
rewritecond_entry *cfg,
char *key, char *val)
{
if ( strcasecmp(key, "nocase") == 0
|| strcasecmp(key, "NC") == 0 ) {
cfg->flags |= CONDFLAG_NOCASE;
}
else if ( strcasecmp(key, "ornext") == 0
|| strcasecmp(key, "OR") == 0 ) {
cfg->flags |= CONDFLAG_ORNEXT;
}
else {
return apr_pstrcat(p, "RewriteCond: unknown flag '", key, "'", NULL);
}
return NULL;
}
static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
const char *in_str)
{
rewrite_perdir_conf *dconf = in_dconf;
char *str = apr_pstrdup(cmd->pool, in_str);
rewrite_server_conf *sconf;
rewriterule_entry *newrule;
regex_t *regexp;
char *a1;
char *a2;
char *a3;
char *cp;
const char *err;
int mode;
sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
/* make a new entry in the internal rewrite rule list */
if (cmd->path == NULL) { /* is server command */
newrule = apr_array_push(sconf->rewriterules);
}
else { /* is per-directory command */
newrule = apr_array_push(dconf->rewriterules);
}
/* parse the argument line ourself */
if (parseargline(str, &a1, &a2, &a3)) {
return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
"'", NULL);
}
/* arg3: optional flags field */
newrule->forced_mimetype = NULL;
newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
newrule->flags = RULEFLAG_NONE;
newrule->env[0] = NULL;
newrule->cookie[0] = NULL;
newrule->skip = 0;
if (a3 != NULL) {
if ((err = cmd_rewriterule_parseflagfield(cmd->pool, newrule,
a3)) != NULL) {
return err;
}
}
/* arg1: the pattern
* try to compile the regexp to test if is ok
*/
cp = a1;
if (cp[0] == '!') {
newrule->flags |= RULEFLAG_NOTMATCH;
cp++;
}
mode = REG_EXTENDED;
if (newrule->flags & RULEFLAG_NOCASE) {
mode |= REG_ICASE;
}
if ((regexp = ap_pregcomp(cmd->pool, cp, mode)) == NULL) {
return apr_pstrcat(cmd->pool,
"RewriteRule: cannot compile regular expression '",
a1, "'", NULL);
}
newrule->pattern = apr_pstrdup(cmd->pool, cp);
newrule->regexp = regexp;
/* arg2: the output string
* replace the $<N> by \<n> which is needed by the currently
* used Regular Expression library
*
* TODO: Is this still required for PCRE? If not, does it *work* with PCRE?
*/
newrule->output = apr_pstrdup(cmd->pool, a2);
/* now, if the server or per-dir config holds an
* array of RewriteCond entries, we take it for us
* and clear the array
*/
if (cmd->path == NULL) { /* is server command */
newrule->rewriteconds = sconf->rewriteconds;
sconf->rewriteconds = apr_array_make(cmd->pool, 2,
sizeof(rewritecond_entry));
}
else { /* is per-directory command */
newrule->rewriteconds = dconf->rewriteconds;
dconf->rewriteconds = apr_array_make(cmd->pool, 2,
sizeof(rewritecond_entry));
}
return NULL;
}
static const char *cmd_rewriterule_parseflagfield(apr_pool_t *p,
rewriterule_entry *cfg,
char *str)
{
char *cp;
char *cp1;
char *cp2;
char *cp3;
char *key;
char *val;
const char *err;
if (str[0] != '[' || str[strlen(str)-1] != ']') {
return "RewriteRule: bad flag delimiters";
}
cp = str+1;
str[strlen(str)-1] = ','; /* for simpler parsing */
for ( ; *cp != '\0'; ) {
/* skip whitespaces */
for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
;
if (*cp == '\0') {
break;
}
cp1 = cp;
if ((cp2 = strchr(cp, ',')) != NULL) {
cp = cp2+1;
for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
;
*cp2 = '\0';
if ((cp3 = strchr(cp1, '=')) != NULL) {
*cp3 = '\0';
key = cp1;
val = cp3+1;
}
else {
key = cp1;
val = "";
}
if ((err = cmd_rewriterule_setflag(p, cfg, key, val)) != NULL) {
return err;
}
}
else {
break;
}
}
return NULL;
}
static const char *cmd_rewriterule_setflag(apr_pool_t *p,
rewriterule_entry *cfg,
char *key, char *val)
{
int status = 0;
int i;
if ( strcasecmp(key, "redirect") == 0
|| strcasecmp(key, "R") == 0 ) {
cfg->flags |= RULEFLAG_FORCEREDIRECT;
if (strlen(val) > 0) {
if (strcasecmp(val, "permanent") == 0) {
status = HTTP_MOVED_PERMANENTLY;
}
else if (strcasecmp(val, "temp") == 0) {
status = HTTP_MOVED_TEMPORARILY;
}
else if (strcasecmp(val, "seeother") == 0) {
status = HTTP_SEE_OTHER;
}
else if (apr_isdigit(*val)) {
status = atoi(val);
}
if (!ap_is_HTTP_REDIRECT(status)) {
return "RewriteRule: invalid HTTP response code "
"for flag 'R'";
}
cfg->forced_responsecode = status;
}
}
else if ( strcasecmp(key, "noescape") == 0
|| strcasecmp(key, "NE") == 0 ) {
cfg->flags |= RULEFLAG_NOESCAPE;
}
else if ( strcasecmp(key, "last") == 0
|| strcasecmp(key, "L") == 0 ) {
cfg->flags |= RULEFLAG_LASTRULE;
}
else if ( strcasecmp(key, "next") == 0
|| strcasecmp(key, "N") == 0 ) {
cfg->flags |= RULEFLAG_NEWROUND;
}
else if ( strcasecmp(key, "chain") == 0
|| strcasecmp(key, "C") == 0 ) {
cfg->flags |= RULEFLAG_CHAIN;
}
else if ( strcasecmp(key, "type") == 0
|| strcasecmp(key, "T") == 0 ) {
cfg->forced_mimetype = apr_pstrdup(p, val);
ap_str_tolower(cfg->forced_mimetype);
}
else if ( strcasecmp(key, "env") == 0
|| strcasecmp(key, "E") == 0 ) {
for (i = 0; (cfg->env[i] != NULL) && (i < MAX_ENV_FLAGS); i++)
;
if (i < MAX_ENV_FLAGS) {
cfg->env[i] = apr_pstrdup(p, val);
cfg->env[i+1] = NULL;
}
else {
return "RewriteRule: too many environment flags 'E'";
}
}
else if ( strcasecmp(key, "cookie") == 0 || strcasecmp(key, "CO") == 0) {
for (i = 0; (cfg->cookie[i] != NULL) && (i < MAX_COOKIE_FLAGS); i++)
;
if (i < MAX_COOKIE_FLAGS) {
cfg->cookie[i] = apr_pstrdup(p, val);
cfg->cookie[i+1] = NULL;
}
else {
return "RewriteRule: too many cookie flags 'CO'";
}
}
else if ( strcasecmp(key, "nosubreq") == 0
|| strcasecmp(key, "NS") == 0 ) {
cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
}
else if ( strcasecmp(key, "proxy") == 0
|| strcasecmp(key, "P") == 0 ) {
cfg->flags |= RULEFLAG_PROXY;
}
else if ( strcasecmp(key, "passthrough") == 0
|| strcasecmp(key, "PT") == 0 ) {
cfg->flags |= RULEFLAG_PASSTHROUGH;
}
else if ( strcasecmp(key, "skip") == 0
|| strcasecmp(key, "S") == 0 ) {
cfg->skip = atoi(val);
}
else if ( strcasecmp(key, "forbidden") == 0
|| strcasecmp(key, "F") == 0 ) {
cfg->flags |= RULEFLAG_FORBIDDEN;
}
else if ( strcasecmp(key, "gone") == 0
|| strcasecmp(key, "G") == 0 ) {
cfg->flags |= RULEFLAG_GONE;
}
else if ( strcasecmp(key, "qsappend") == 0
|| strcasecmp(key, "QSA") == 0 ) {
cfg->flags |= RULEFLAG_QSAPPEND;
}
else if ( strcasecmp(key, "nocase") == 0
|| strcasecmp(key, "NC") == 0 ) {
cfg->flags |= RULEFLAG_NOCASE;
}
else {
return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL);
}
return NULL;
}
/*
**
** Global Module Initialization
**
*/
static int pre_config(apr_pool_t *pconf,
apr_pool_t *plog,
apr_pool_t *ptemp)
{
APR_OPTIONAL_FN_TYPE(ap_register_rewrite_mapfunc) *map_pfn_register;
/* register int: rewritemap handlers */
mapfunc_hash = apr_hash_make(pconf);
map_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_rewrite_mapfunc);
if (map_pfn_register) {
map_pfn_register("tolower", rewrite_mapfunc_tolower);
map_pfn_register("toupper", rewrite_mapfunc_toupper);
map_pfn_register("escape", rewrite_mapfunc_escape);