diff options
Diffstat (limited to 'utility/util.c')
-rw-r--r-- | utility/util.c | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/utility/util.c b/utility/util.c new file mode 100644 index 0000000..dcfb028 --- /dev/null +++ b/utility/util.c | |||
@@ -0,0 +1,189 @@ | |||
1 | #include "util.h" | ||
2 | #include "apr_strings.h" | ||
3 | #include "apr_lib.h" | ||
4 | #include "apr_file_io.h" | ||
5 | #include "apr_time.h" | ||
6 | |||
7 | #include "config.h" | ||
8 | |||
9 | #include <stdarg.h> | ||
10 | |||
11 | char *lowerstr(apr_pool_t *pool, const char *input) | ||
12 | { | ||
13 | char *temp; | ||
14 | char *itr; | ||
15 | temp = apr_pstrdup(pool, input); | ||
16 | for (itr=temp; *itr!='\0'; itr++) { | ||
17 | *itr = apr_tolower(*itr); | ||
18 | } | ||
19 | return temp; | ||
20 | } | ||
21 | |||
22 | void line_chomp(char *str) | ||
23 | { | ||
24 | int len; | ||
25 | // chomp off newline | ||
26 | len = strlen(str); | ||
27 | if (len) { | ||
28 | while (str[len-1] == '\r' || str[len-1] == '\n') { | ||
29 | str[len-1] = '\0'; | ||
30 | len--; | ||
31 | } | ||
32 | } | ||
33 | } | ||
34 | |||
35 | /* | ||
36 | * *** Ripped from HTTPD util.c (why are so many PORTABLE things not in APR UTIL?) | ||
37 | */ | ||
38 | static char x2c(const char *what) | ||
39 | { | ||
40 | register char digit; | ||
41 | |||
42 | digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 | ||
43 | : (what[0] - '0')); | ||
44 | digit *= 16; | ||
45 | digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 | ||
46 | : (what[1] - '0')); | ||
47 | return (digit); | ||
48 | } | ||
49 | |||
50 | /* | ||
51 | * *** Ripped from HTTPD util.c (why are so many PORTABLE things not in APR UTIL?) | ||
52 | * | ||
53 | * Unescapes a URL, leaving reserved characters intact. | ||
54 | * Returns 0 on success, non-zero on error | ||
55 | * Failure is due to | ||
56 | * bad % escape returns HTTP_BAD_REQUEST | ||
57 | * | ||
58 | * decoding %00 or a forbidden character returns HTTP_NOT_FOUND | ||
59 | */ | ||
60 | static int unescape_url(char *url, const char *forbid, const char *reserved) | ||
61 | { | ||
62 | register int badesc, badpath; | ||
63 | char *x, *y; | ||
64 | |||
65 | badesc = 0; | ||
66 | badpath = 0; | ||
67 | /* Initial scan for first '%'. Don't bother writing values before | ||
68 | * seeing a '%' */ | ||
69 | y = strchr(url, '%'); | ||
70 | if (y == NULL) { | ||
71 | return APR_SUCCESS; | ||
72 | } | ||
73 | for (x = y; *y; ++x, ++y) { | ||
74 | if (*y != '%') { | ||
75 | *x = *y; | ||
76 | } | ||
77 | else { | ||
78 | if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) { | ||
79 | badesc = 1; | ||
80 | *x = '%'; | ||
81 | } | ||
82 | else { | ||
83 | char decoded; | ||
84 | decoded = x2c(y + 1); | ||
85 | if ((decoded == '\0') | ||
86 | || (forbid && strchr(forbid, decoded))) { | ||
87 | badpath = 1; | ||
88 | *x = decoded; | ||
89 | y += 2; | ||
90 | } | ||
91 | else if (reserved && strchr(reserved, decoded)) { | ||
92 | *x++ = *y++; | ||
93 | *x++ = *y++; | ||
94 | *x = *y; | ||
95 | } | ||
96 | else { | ||
97 | *x = decoded; | ||
98 | y += 2; | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | *x = '\0'; | ||
104 | if (badesc) { | ||
105 | return APR_EINVAL; | ||
106 | } | ||
107 | else if (badpath) { | ||
108 | return APR_EINVAL; | ||
109 | } | ||
110 | else { | ||
111 | return APR_SUCCESS; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | /* | ||
116 | * *** Ripped from HTTPD util.c (why are so many PORTABLE things not in APR UTIL?) | ||
117 | */ | ||
118 | int ap_unescape_url(char *url) | ||
119 | { | ||
120 | /* Traditional */ | ||
121 | #ifdef CASE_BLIND_FILESYSTEM | ||
122 | return unescape_url(url, "/\\", NULL); | ||
123 | #else | ||
124 | return unescape_url(url, "/", NULL); | ||
125 | #endif | ||
126 | } | ||
127 | |||
128 | void logging_preinit(config_t *cfg) | ||
129 | { | ||
130 | apr_pool_create(&cfg->errorlog_p, cfg->pool); | ||
131 | apr_file_open_stderr(&cfg->errorlog_fperr, cfg->pool); | ||
132 | } | ||
133 | |||
134 | void logging_init(config_t *cfg) | ||
135 | { | ||
136 | apr_status_t rv; | ||
137 | if (cfg->errorlog) { | ||
138 | rv = apr_file_open(&cfg->errorlog_fp, cfg->errorlog, | ||
139 | APR_FOPEN_CREATE | APR_FOPEN_WRITE | APR_FOPEN_APPEND, | ||
140 | APR_OS_DEFAULT, | ||
141 | cfg->pool); | ||
142 | if (rv) { | ||
143 | printf("Error opening %s\n",cfg->errorlog); | ||
144 | cfg->loglevel = LOGLEVEL_NONE; | ||
145 | } | ||
146 | logging_log(cfg, LOGLEVEL_ERROR, "Log file Opened"); | ||
147 | } else { | ||
148 | cfg->loglevel = LOGLEVEL_NONE; | ||
149 | logging_log(cfg, LOGLEVEL_NOISE, "No Log file specified, disabled logging"); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | const char *logging_strerror(apr_status_t rv) | ||
154 | { | ||
155 | char buff[256]; | ||
156 | return apr_strerror(rv, buff, 256); | ||
157 | } | ||
158 | |||
159 | void logging_log(config_t *cfg, loglevel_e level, const char *fmt, ...) | ||
160 | { | ||
161 | va_list ap; | ||
162 | char date[APR_RFC822_DATE_LEN]; | ||
163 | struct iovec vec[4]; | ||
164 | apr_size_t blen; | ||
165 | |||
166 | if (cfg->loglevel < level) return; | ||
167 | |||
168 | va_start(ap, fmt); | ||
169 | apr_pool_clear(cfg->errorlog_p); | ||
170 | |||
171 | apr_rfc822_date(date, apr_time_now()); | ||
172 | vec[0].iov_base = date; | ||
173 | vec[0].iov_len = APR_RFC822_DATE_LEN-1; | ||
174 | vec[1].iov_base = " "; | ||
175 | vec[1].iov_len = 2; | ||
176 | vec[2].iov_base = apr_pvsprintf(cfg->errorlog_p, fmt, ap); | ||
177 | vec[2].iov_len = strlen(vec[2].iov_base); | ||
178 | vec[3].iov_base = "\n"; | ||
179 | vec[3].iov_len = 1; | ||
180 | |||
181 | if (level == LOGLEVEL_NOISE) { | ||
182 | apr_file_writev(cfg->errorlog_fperr,&vec[2],2,&blen); | ||
183 | } | ||
184 | if (cfg->loglevel > LOGLEVEL_NONE && cfg->errorlog_fp) { | ||
185 | apr_file_writev(cfg->errorlog_fp,vec,4,&blen); | ||
186 | } | ||
187 | |||
188 | va_end(ap); | ||
189 | } | ||