]> err.no Git - sope/blob - Recycler/mod_objc/ApacheRequest.m
use %p for pointer formats
[sope] / Recycler / mod_objc / ApacheRequest.m
1 // $Id: ApacheRequest.m,v 1.1 2004/06/08 11:15:59 helge Exp $
2
3 #include "ApacheRequest.h"
4 #import <Foundation/Foundation.h>
5 #include "httpd.h"
6 #include "http_request.h"
7 #include "http_protocol.h"
8
9 #define ApCharSetAccessor(_field, _val_) \
10   {\
11     char *val;\
12     unsigned len;\
13     \
14     len = [_val_ cStringLength];\
15     val = ap_palloc(AP_HANDLE->pool, len + 1);\
16     [_val_ getCString:val];\
17     val[len] = '\0';\
18     \
19     AP_HANDLE->_field = val;\
20   }
21
22 @implementation ApacheRequest
23 #define AP_HANDLE ((request_rec *)self->handle)
24
25 /* accessors */
26
27 - (ApacheResourcePool *)requestPool {
28   return [ApacheResourcePool objectWithHandle:AP_HANDLE->pool];
29 }
30 - (ApacheConnection *)connection {
31   return [ApacheConnection objectWithHandle:AP_HANDLE->connection];
32 }
33 - (ApacheServer *)server {
34   return [ApacheServer objectWithHandle:AP_HANDLE->server];
35 }
36
37 /* requests */
38
39 - (ApacheRequest *)redirectToRequest {
40   return [ApacheRequest objectWithHandle:AP_HANDLE->next];
41 }
42 - (ApacheRequest *)redirectFromRequest {
43   return [ApacheRequest objectWithHandle:AP_HANDLE->prev];
44 }
45 - (ApacheRequest *)mainRequest {
46   return [ApacheRequest objectWithHandle:AP_HANDLE->main];
47 }
48
49 /*
50   Info about the request itself... we begin with stuff that only
51   protocol.c should ever touch...
52 */
53
54 - (NSString *)firstRequestLine {
55   return [NSString stringWithCString:AP_HANDLE->the_request];
56 }
57 - (BOOL)isBackwards {
58   return AP_HANDLE->assbackwards ? YES : NO;
59 }
60 /* proxy ? */
61 - (BOOL)isHeadRequest {
62   return AP_HANDLE->header_only ? YES : NO;
63 }
64
65 - (NSString *)protocol {
66   return [NSString stringWithCString:AP_HANDLE->protocol];
67 }
68 - (int)protocolNumber {
69   return AP_HANDLE->proto_num;
70 }
71
72 - (NSString *)hostName {
73   return [NSString stringWithCString:AP_HANDLE->hostname];
74 }
75
76 - (NSDate *)requestTime {
77   return [NSDate dateWithTimeIntervalSince1970:AP_HANDLE->request_time];
78 }
79
80 - (NSString *)statusLine {
81   return [NSString stringWithCString:AP_HANDLE->status_line];
82 }
83 - (int)status {
84   return AP_HANDLE->status;
85 }
86
87 /*
88   Request method, two ways; also, protocol, etc..  Outside of protocol.c,
89   look, but don't touch.
90 */
91 - (NSString *)method {
92   return [NSString stringWithCString:AP_HANDLE->method];
93 }
94 - (int)methodNumber {
95   return AP_HANDLE->method_number;
96 }
97 + (int)numberForMethod:(NSString *)_method {
98   return ap_method_number_of([_method cString]);
99 }
100
101 - (void)allowMethodNumber:(int)_num {
102   AP_HANDLE->allowed |= (1 << _num);
103 }
104 - (BOOL)isMethodNumberAllowed:(int)_num {
105   return (AP_HANDLE->allowed & (1 << _num)) ? YES : NO;
106 }
107
108 - (unsigned int)bytesSent {
109   return AP_HANDLE->bytes_sent;
110 }
111 - (NSDate *)lastModified {
112   return [NSDate dateWithTimeIntervalSince1970:AP_HANDLE->mtime];
113 }
114
115 /* HTTP/1.1 connection-level features */
116
117 - (BOOL)isChunkedSending {
118   return AP_HANDLE->chunked ? YES : NO;
119 }
120 - (int)byteRangeCount {
121   return AP_HANDLE->byterange;
122 }
123 - (NSString *)byteRangeBoundary {
124   return [NSString stringWithCString:AP_HANDLE->boundary];
125 }
126 - (NSString *)range {
127   return [NSString stringWithCString:AP_HANDLE->range];
128 }
129 - (unsigned int)contentLength {
130   return AP_HANDLE->clength;
131 }
132
133 - (unsigned int)numberOfRemainingBytes {
134   return AP_HANDLE->remaining;
135 }
136 - (unsigned int)numberOfReadBytes {
137   return AP_HANDLE->read_length;
138 }
139 - (BOOL)isChunkedReceiving {
140   return AP_HANDLE->read_chunked ? YES : NO;
141 }
142 - (BOOL)isExpecting100 {
143   return AP_HANDLE->expecting_100 ? YES : NO;
144 }
145
146 /*
147   MIME header environments, in and out.  Also, an array containing
148   environment variables to be passed to subprocesses, so people can
149   write modules to add to that environment.
150
151   The difference between headers_out and err_headers_out is that the
152   latter are printed even on error, and persist across internal redirects
153   (so the headers printed for ErrorDocument handlers will have them).
154
155   The 'notes' table is for notes from one module to another, with no
156   other set purpose in mind...
157 */
158
159 - (ApacheTable *)headersIn {
160   return [ApacheTable objectWithHandle:AP_HANDLE->headers_in];
161 }
162 - (ApacheTable *)headersOut {
163   return [ApacheTable objectWithHandle:AP_HANDLE->headers_out];
164 }
165 - (ApacheTable *)errorHeadersOut {
166   return [ApacheTable objectWithHandle:AP_HANDLE->err_headers_out];
167 }
168 - (ApacheTable *)subprocessEnvironment {
169   return [ApacheTable objectWithHandle:AP_HANDLE->subprocess_env];
170 }
171 - (ApacheTable *)notes {
172   return [ApacheTable objectWithHandle:AP_HANDLE->notes];
173 }
174
175 /*
176   content_type, handler, content_encoding, content_language, and all
177   content_languages MUST be lowercased strings.  They may be pointers
178   to static strings; they should not be modified in place.
179 */
180
181 - (void)setContentType:(NSString *)_ctype {
182   _ctype = [_ctype lowercaseString];
183   ApCharSetAccessor(content_type, _ctype);
184 }
185 - (NSString *)contentType {
186   return [NSString stringWithCString:AP_HANDLE->content_type];
187 }
188
189 - (void)setContentEncoding:(NSString *)_cencoding {
190   _cencoding = [_cencoding lowercaseString];
191   ApCharSetAccessor(content_encoding, _cencoding);
192 }
193 - (NSString *)contentEncoding {
194   return [NSString stringWithCString:AP_HANDLE->content_encoding];
195 }
196
197 - (void)setContentLanguage:(NSString *)_clanguage {
198   _clanguage = [_clanguage lowercaseString];
199   ApCharSetAccessor(content_language, _clanguage);
200 }
201 - (NSString *)contentLanguage {
202   return [NSString stringWithCString:AP_HANDLE->content_language];
203 }
204
205 - (NSString *)vlistValidator {
206   return [NSString stringWithCString:AP_HANDLE->vlist_validator];
207 }
208
209 - (NSArray *)contentLanguages {
210   // array_header *content_languages;   /* array of (char*) */
211   
212   return [self notImplemented:_cmd];
213 }
214
215 - (void)setHandler:(NSString *)_value {
216   ApCharSetAccessor(handler, _value);
217 }
218 - (NSString *)handler {
219   return [NSString stringWithCString:AP_HANDLE->handler];
220 }
221
222 - (BOOL)noCache {
223   return AP_HANDLE->no_cache ? YES : NO;
224 }
225 - (BOOL)noLocalCopy {
226   return AP_HANDLE->no_local_copy ? YES : NO;
227 }
228
229 /*
230   What object is being requested (either directly, or via include
231   or content-negotiation mapping).
232 */
233 - (NSString *)unparsedURI {
234   return [NSString stringWithCString:AP_HANDLE->unparsed_uri];
235 }
236 - (NSString *)uri {
237   return [NSString stringWithCString:AP_HANDLE->uri];
238 }
239 - (NSString *)filename {
240   return [NSString stringWithCString:AP_HANDLE->filename];
241 }
242 - (NSString *)pathInfo {
243   return [NSString stringWithCString:AP_HANDLE->path_info];
244 }
245 - (NSString *)queryArgs {
246   return [NSString stringWithCString:AP_HANDLE->args];
247 }
248 // finfo, parse_uri
249
250 - (NSString *)casePreservedFilename {
251   return [NSString stringWithCString:AP_HANDLE->case_preserved_filename];
252 }
253
254 - (void)parseURI:(NSString *)_uri {
255   ap_parse_uri(AP_HANDLE, [_uri cString]);
256 }
257
258 /* sub-requests */
259
260 - (ApacheRequest *)subRequestLookupURI:(NSString *)_newfile {
261   request_rec *sr;
262   sr = ap_sub_req_lookup_uri([_newfile cString], AP_HANDLE);
263   return [ApacheRequest objectWithHandle:sr];
264 }
265 - (ApacheRequest *)subRequestLookupURI:(NSString *)_newfile
266   method:(NSString *)_method
267 {
268   request_rec *sr;
269   sr = ap_sub_req_method_uri([_method cString], [_newfile cString], AP_HANDLE);
270   return [ApacheRequest objectWithHandle:sr];
271 }
272 - (ApacheRequest *)subRequestLookupFile:(NSString *)_newfile {
273   request_rec *sr;
274   sr = ap_sub_req_lookup_file([_newfile cString], AP_HANDLE);
275   return [ApacheRequest objectWithHandle:sr];
276 }
277
278 /* operations */
279
280 - (int)runSubRequest {
281   return ap_run_sub_req(AP_HANDLE);
282 }
283 - (void)destroySubRequest {
284   ap_destroy_sub_req(AP_HANDLE);
285 }
286
287 - (void)internalRedirect:(NSString *)_uri {
288   ap_internal_redirect([_uri cString], AP_HANDLE);
289 }
290 - (void)internalRedirectHandler:(NSString *)_uri {
291   ap_internal_redirect_handler([_uri cString], AP_HANDLE);
292 }
293
294 - (int)someAuthorizationRequired {
295   return ap_some_auth_required(AP_HANDLE);
296 }
297 - (BOOL)isInitialRequest {
298   return ap_is_initial_req(AP_HANDLE);
299 }
300
301 - (NSDate *)updateModificationTime:(NSDate *)_date {
302   return [NSDate dateWithTimeIntervalSince1970:
303                    ap_update_mtime(AP_HANDLE, [_date timeIntervalSince1970])];
304 }
305
306 /* sending headers */
307
308 - (void)sendBasicHttpHeader {
309   ap_basic_http_header(AP_HANDLE);
310 }
311 - (void)sendHttpHeader {
312   ap_send_http_header(AP_HANDLE);
313 }
314 - (int)sendHttpTrace {
315   return ap_send_http_trace(AP_HANDLE);
316 }
317 - (int)sendHttpOptions {
318   return ap_send_http_options(AP_HANDLE);
319 }
320
321 - (void)finalizeRequestProtocol {
322   ap_finalize_request_protocol(AP_HANDLE);
323 }
324
325 - (void)sendErrorResponse {
326   [self sendErrorResponseWithRecursiveFailStatus:500];
327 }
328 - (void)sendErrorResponseWithRecursiveFailStatus:(int)_state {
329   ap_send_error_response(AP_HANDLE, _state);
330 }
331
332 /* modifying headers */
333
334 - (int)setContentLength:(unsigned int)_len {
335   return ap_set_content_length(AP_HANDLE, _len);
336 }
337 - (int)setKeepAlive {
338   return ap_set_keepalive(AP_HANDLE);
339 }
340 - (NSDate *)rationalizeModificationTime:(NSDate *)_mtime {
341   time_t t;
342   t = ap_rationalize_mtime(AP_HANDLE, [_mtime timeIntervalSince1970]);
343   return [NSDate dateWithTimeIntervalSince1970:t];
344 }
345 - (NSString *)makeETag:(BOOL)_forceWeak {
346   return [NSString stringWithCString:ap_make_etag(AP_HANDLE, _forceWeak)];
347 }
348 - (void)setETag {
349   ap_set_etag(AP_HANDLE);
350 }
351 - (void)setLastModified {
352   ap_set_last_modified(AP_HANDLE);
353 }
354 - (int)meetsConditions {
355   return ap_meets_conditions(AP_HANDLE);
356 }
357
358 /* sending content */
359
360 - (long)sendFile:(FILE *)_file {
361   return ap_send_fd(_file, AP_HANDLE);
362 }
363 - (long)sendFile:(FILE *)_file length:(long)_len {
364   return ap_send_fd_length(_file, AP_HANDLE, _len);
365 }
366 - (unsigned int)sendMMap:(void *)_mm
367   offset:(unsigned int)_off length:(unsigned int)_len
368 {
369   return ap_send_mmap(_mm, AP_HANDLE, _off, _len);
370 }
371
372 - (int)rputc:(int)_c {
373   return ap_rputc(_c, AP_HANDLE);
374 }
375 - (int)rputs:(const char *)_cstr {
376   return ap_rputs(_cstr, AP_HANDLE);
377 }
378 - (int)rwrite:(const void *)_buf length:(unsigned int)_len {
379   return ap_rwrite(_buf, _len, AP_HANDLE);
380 }
381 - (int)rflush {
382   return ap_rflush(AP_HANDLE);
383 }
384
385 - (int)rwriteData:(NSData *)_data {
386   return ap_rwrite([_data bytes], [_data length], AP_HANDLE);
387 }
388
389 /* Reading a block of data from the client connection (e.g., POST arg) */
390
391 - (int)setupClientBlock:(int)_readPolicy {
392   return ap_setup_client_block(AP_HANDLE, _readPolicy);
393 }
394 - (int)shouldClientBlock {
395   return ap_should_client_block(AP_HANDLE);
396 }
397 - (long)getClientBlock:(char *)_buffer length:(int)_bufsiz {
398   return ap_get_client_block(AP_HANDLE, _buffer, _bufsiz);
399 }
400 - (int)discardRequestBody {
401   return ap_discard_request_body(AP_HANDLE);
402 }
403
404 /* Sending a byterange */
405
406 - (int)setByteRange {
407   return ap_set_byterange(AP_HANDLE);
408 }
409 // ap_each_byterange(request_rec *r, long *offset, long *length);
410
411 /* basic authentication */
412
413 - (void)noteAuthFailure {
414   ap_note_auth_failure(AP_HANDLE);
415 }
416 - (void)noteBasicAuthFailure {
417   ap_note_basic_auth_failure(AP_HANDLE);
418 }
419 - (int)getBasicAuthPassword:(const char **)_pwd {
420   return ap_get_basic_auth_pw(AP_HANDLE, _pwd);
421 }
422
423 #undef AP_HANDLE
424
425 - (NSString *)description {
426   NSMutableString *ms;
427   id tmp;
428   
429   ms = [NSMutableString stringWithCapacity:128];
430   [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
431   [ms appendFormat:@" 0x%p", self->handle];
432   
433   if ([self isHeadRequest]) [ms appendString:@" head"];
434   
435   tmp = [self method];
436   if ([tmp length] > 0) [ms appendFormat:@" %@", tmp];
437   tmp = [self uri];
438   if ([tmp length] > 0) [ms appendFormat:@" uri='%@'", tmp];
439
440   if ([self isChunkedReceiving]) [ms appendString:@" in-chunked"];
441   if ([self isChunkedSending])   [ms appendString:@" out-chunked"];
442   
443   if ([self numberOfReadBytes] > 0)
444     [ms appendFormat:@" bytesRead=%i", [self numberOfReadBytes]];
445   if ([self numberOfRemainingBytes] > 0)
446     [ms appendFormat:@" remainingBytes=%i", [self numberOfRemainingBytes]];
447   if ([self bytesSent] > 0)
448     [ms appendFormat:@" bytesSent=%i", [self bytesSent]];
449
450   if ((tmp = [self connection]))
451     [ms appendFormat:@" con=%@", tmp];
452   
453   [ms appendString:@">"];
454   return ms;
455 }
456
457 @end /* ApacheRequest */