1 // $Id: ApacheRequest.m,v 1.1 2004/06/08 11:15:59 helge Exp $
3 #include "ApacheRequest.h"
4 #import <Foundation/Foundation.h>
6 #include "http_request.h"
7 #include "http_protocol.h"
9 #define ApCharSetAccessor(_field, _val_) \
14 len = [_val_ cStringLength];\
15 val = ap_palloc(AP_HANDLE->pool, len + 1);\
16 [_val_ getCString:val];\
19 AP_HANDLE->_field = val;\
22 @implementation ApacheRequest
23 #define AP_HANDLE ((request_rec *)self->handle)
27 - (ApacheResourcePool *)requestPool {
28 return [ApacheResourcePool objectWithHandle:AP_HANDLE->pool];
30 - (ApacheConnection *)connection {
31 return [ApacheConnection objectWithHandle:AP_HANDLE->connection];
33 - (ApacheServer *)server {
34 return [ApacheServer objectWithHandle:AP_HANDLE->server];
39 - (ApacheRequest *)redirectToRequest {
40 return [ApacheRequest objectWithHandle:AP_HANDLE->next];
42 - (ApacheRequest *)redirectFromRequest {
43 return [ApacheRequest objectWithHandle:AP_HANDLE->prev];
45 - (ApacheRequest *)mainRequest {
46 return [ApacheRequest objectWithHandle:AP_HANDLE->main];
50 Info about the request itself... we begin with stuff that only
51 protocol.c should ever touch...
54 - (NSString *)firstRequestLine {
55 return [NSString stringWithCString:AP_HANDLE->the_request];
58 return AP_HANDLE->assbackwards ? YES : NO;
61 - (BOOL)isHeadRequest {
62 return AP_HANDLE->header_only ? YES : NO;
65 - (NSString *)protocol {
66 return [NSString stringWithCString:AP_HANDLE->protocol];
68 - (int)protocolNumber {
69 return AP_HANDLE->proto_num;
72 - (NSString *)hostName {
73 return [NSString stringWithCString:AP_HANDLE->hostname];
76 - (NSDate *)requestTime {
77 return [NSDate dateWithTimeIntervalSince1970:AP_HANDLE->request_time];
80 - (NSString *)statusLine {
81 return [NSString stringWithCString:AP_HANDLE->status_line];
84 return AP_HANDLE->status;
88 Request method, two ways; also, protocol, etc.. Outside of protocol.c,
89 look, but don't touch.
91 - (NSString *)method {
92 return [NSString stringWithCString:AP_HANDLE->method];
95 return AP_HANDLE->method_number;
97 + (int)numberForMethod:(NSString *)_method {
98 return ap_method_number_of([_method cString]);
101 - (void)allowMethodNumber:(int)_num {
102 AP_HANDLE->allowed |= (1 << _num);
104 - (BOOL)isMethodNumberAllowed:(int)_num {
105 return (AP_HANDLE->allowed & (1 << _num)) ? YES : NO;
108 - (unsigned int)bytesSent {
109 return AP_HANDLE->bytes_sent;
111 - (NSDate *)lastModified {
112 return [NSDate dateWithTimeIntervalSince1970:AP_HANDLE->mtime];
115 /* HTTP/1.1 connection-level features */
117 - (BOOL)isChunkedSending {
118 return AP_HANDLE->chunked ? YES : NO;
120 - (int)byteRangeCount {
121 return AP_HANDLE->byterange;
123 - (NSString *)byteRangeBoundary {
124 return [NSString stringWithCString:AP_HANDLE->boundary];
126 - (NSString *)range {
127 return [NSString stringWithCString:AP_HANDLE->range];
129 - (unsigned int)contentLength {
130 return AP_HANDLE->clength;
133 - (unsigned int)numberOfRemainingBytes {
134 return AP_HANDLE->remaining;
136 - (unsigned int)numberOfReadBytes {
137 return AP_HANDLE->read_length;
139 - (BOOL)isChunkedReceiving {
140 return AP_HANDLE->read_chunked ? YES : NO;
142 - (BOOL)isExpecting100 {
143 return AP_HANDLE->expecting_100 ? YES : NO;
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.
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).
155 The 'notes' table is for notes from one module to another, with no
156 other set purpose in mind...
159 - (ApacheTable *)headersIn {
160 return [ApacheTable objectWithHandle:AP_HANDLE->headers_in];
162 - (ApacheTable *)headersOut {
163 return [ApacheTable objectWithHandle:AP_HANDLE->headers_out];
165 - (ApacheTable *)errorHeadersOut {
166 return [ApacheTable objectWithHandle:AP_HANDLE->err_headers_out];
168 - (ApacheTable *)subprocessEnvironment {
169 return [ApacheTable objectWithHandle:AP_HANDLE->subprocess_env];
171 - (ApacheTable *)notes {
172 return [ApacheTable objectWithHandle:AP_HANDLE->notes];
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.
181 - (void)setContentType:(NSString *)_ctype {
182 _ctype = [_ctype lowercaseString];
183 ApCharSetAccessor(content_type, _ctype);
185 - (NSString *)contentType {
186 return [NSString stringWithCString:AP_HANDLE->content_type];
189 - (void)setContentEncoding:(NSString *)_cencoding {
190 _cencoding = [_cencoding lowercaseString];
191 ApCharSetAccessor(content_encoding, _cencoding);
193 - (NSString *)contentEncoding {
194 return [NSString stringWithCString:AP_HANDLE->content_encoding];
197 - (void)setContentLanguage:(NSString *)_clanguage {
198 _clanguage = [_clanguage lowercaseString];
199 ApCharSetAccessor(content_language, _clanguage);
201 - (NSString *)contentLanguage {
202 return [NSString stringWithCString:AP_HANDLE->content_language];
205 - (NSString *)vlistValidator {
206 return [NSString stringWithCString:AP_HANDLE->vlist_validator];
209 - (NSArray *)contentLanguages {
210 // array_header *content_languages; /* array of (char*) */
212 return [self notImplemented:_cmd];
215 - (void)setHandler:(NSString *)_value {
216 ApCharSetAccessor(handler, _value);
218 - (NSString *)handler {
219 return [NSString stringWithCString:AP_HANDLE->handler];
223 return AP_HANDLE->no_cache ? YES : NO;
225 - (BOOL)noLocalCopy {
226 return AP_HANDLE->no_local_copy ? YES : NO;
230 What object is being requested (either directly, or via include
231 or content-negotiation mapping).
233 - (NSString *)unparsedURI {
234 return [NSString stringWithCString:AP_HANDLE->unparsed_uri];
237 return [NSString stringWithCString:AP_HANDLE->uri];
239 - (NSString *)filename {
240 return [NSString stringWithCString:AP_HANDLE->filename];
242 - (NSString *)pathInfo {
243 return [NSString stringWithCString:AP_HANDLE->path_info];
245 - (NSString *)queryArgs {
246 return [NSString stringWithCString:AP_HANDLE->args];
250 - (NSString *)casePreservedFilename {
251 return [NSString stringWithCString:AP_HANDLE->case_preserved_filename];
254 - (void)parseURI:(NSString *)_uri {
255 ap_parse_uri(AP_HANDLE, [_uri cString]);
260 - (ApacheRequest *)subRequestLookupURI:(NSString *)_newfile {
262 sr = ap_sub_req_lookup_uri([_newfile cString], AP_HANDLE);
263 return [ApacheRequest objectWithHandle:sr];
265 - (ApacheRequest *)subRequestLookupURI:(NSString *)_newfile
266 method:(NSString *)_method
269 sr = ap_sub_req_method_uri([_method cString], [_newfile cString], AP_HANDLE);
270 return [ApacheRequest objectWithHandle:sr];
272 - (ApacheRequest *)subRequestLookupFile:(NSString *)_newfile {
274 sr = ap_sub_req_lookup_file([_newfile cString], AP_HANDLE);
275 return [ApacheRequest objectWithHandle:sr];
280 - (int)runSubRequest {
281 return ap_run_sub_req(AP_HANDLE);
283 - (void)destroySubRequest {
284 ap_destroy_sub_req(AP_HANDLE);
287 - (void)internalRedirect:(NSString *)_uri {
288 ap_internal_redirect([_uri cString], AP_HANDLE);
290 - (void)internalRedirectHandler:(NSString *)_uri {
291 ap_internal_redirect_handler([_uri cString], AP_HANDLE);
294 - (int)someAuthorizationRequired {
295 return ap_some_auth_required(AP_HANDLE);
297 - (BOOL)isInitialRequest {
298 return ap_is_initial_req(AP_HANDLE);
301 - (NSDate *)updateModificationTime:(NSDate *)_date {
302 return [NSDate dateWithTimeIntervalSince1970:
303 ap_update_mtime(AP_HANDLE, [_date timeIntervalSince1970])];
306 /* sending headers */
308 - (void)sendBasicHttpHeader {
309 ap_basic_http_header(AP_HANDLE);
311 - (void)sendHttpHeader {
312 ap_send_http_header(AP_HANDLE);
314 - (int)sendHttpTrace {
315 return ap_send_http_trace(AP_HANDLE);
317 - (int)sendHttpOptions {
318 return ap_send_http_options(AP_HANDLE);
321 - (void)finalizeRequestProtocol {
322 ap_finalize_request_protocol(AP_HANDLE);
325 - (void)sendErrorResponse {
326 [self sendErrorResponseWithRecursiveFailStatus:500];
328 - (void)sendErrorResponseWithRecursiveFailStatus:(int)_state {
329 ap_send_error_response(AP_HANDLE, _state);
332 /* modifying headers */
334 - (int)setContentLength:(unsigned int)_len {
335 return ap_set_content_length(AP_HANDLE, _len);
337 - (int)setKeepAlive {
338 return ap_set_keepalive(AP_HANDLE);
340 - (NSDate *)rationalizeModificationTime:(NSDate *)_mtime {
342 t = ap_rationalize_mtime(AP_HANDLE, [_mtime timeIntervalSince1970]);
343 return [NSDate dateWithTimeIntervalSince1970:t];
345 - (NSString *)makeETag:(BOOL)_forceWeak {
346 return [NSString stringWithCString:ap_make_etag(AP_HANDLE, _forceWeak)];
349 ap_set_etag(AP_HANDLE);
351 - (void)setLastModified {
352 ap_set_last_modified(AP_HANDLE);
354 - (int)meetsConditions {
355 return ap_meets_conditions(AP_HANDLE);
358 /* sending content */
360 - (long)sendFile:(FILE *)_file {
361 return ap_send_fd(_file, AP_HANDLE);
363 - (long)sendFile:(FILE *)_file length:(long)_len {
364 return ap_send_fd_length(_file, AP_HANDLE, _len);
366 - (unsigned int)sendMMap:(void *)_mm
367 offset:(unsigned int)_off length:(unsigned int)_len
369 return ap_send_mmap(_mm, AP_HANDLE, _off, _len);
372 - (int)rputc:(int)_c {
373 return ap_rputc(_c, AP_HANDLE);
375 - (int)rputs:(const char *)_cstr {
376 return ap_rputs(_cstr, AP_HANDLE);
378 - (int)rwrite:(const void *)_buf length:(unsigned int)_len {
379 return ap_rwrite(_buf, _len, AP_HANDLE);
382 return ap_rflush(AP_HANDLE);
385 - (int)rwriteData:(NSData *)_data {
386 return ap_rwrite([_data bytes], [_data length], AP_HANDLE);
389 /* Reading a block of data from the client connection (e.g., POST arg) */
391 - (int)setupClientBlock:(int)_readPolicy {
392 return ap_setup_client_block(AP_HANDLE, _readPolicy);
394 - (int)shouldClientBlock {
395 return ap_should_client_block(AP_HANDLE);
397 - (long)getClientBlock:(char *)_buffer length:(int)_bufsiz {
398 return ap_get_client_block(AP_HANDLE, _buffer, _bufsiz);
400 - (int)discardRequestBody {
401 return ap_discard_request_body(AP_HANDLE);
404 /* Sending a byterange */
406 - (int)setByteRange {
407 return ap_set_byterange(AP_HANDLE);
409 // ap_each_byterange(request_rec *r, long *offset, long *length);
411 /* basic authentication */
413 - (void)noteAuthFailure {
414 ap_note_auth_failure(AP_HANDLE);
416 - (void)noteBasicAuthFailure {
417 ap_note_basic_auth_failure(AP_HANDLE);
419 - (int)getBasicAuthPassword:(const char **)_pwd {
420 return ap_get_basic_auth_pw(AP_HANDLE, _pwd);
425 - (NSString *)description {
429 ms = [NSMutableString stringWithCapacity:128];
430 [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
431 [ms appendFormat:@" 0x%p", self->handle];
433 if ([self isHeadRequest]) [ms appendString:@" head"];
436 if ([tmp length] > 0) [ms appendFormat:@" %@", tmp];
438 if ([tmp length] > 0) [ms appendFormat:@" uri='%@'", tmp];
440 if ([self isChunkedReceiving]) [ms appendString:@" in-chunked"];
441 if ([self isChunkedSending]) [ms appendString:@" out-chunked"];
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]];
450 if ((tmp = [self connection]))
451 [ms appendFormat:@" con=%@", tmp];
453 [ms appendString:@">"];
457 @end /* ApacheRequest */