2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <NGObjWeb/WOProxyRequestHandler.h>
24 #include <NGObjWeb/WOApplication.h>
25 #include <NGObjWeb/WORequest.h>
26 #include <NGObjWeb/WOResponse.h>
27 #include <NGObjWeb/WOHTTPConnection.h>
30 @implementation WOProxyRequestHandler
33 return [super version] + 0 /* v2 */;
36 NSAssert2([super version] == 2,
37 @"invalid superclass (%@) version %i !",
38 NSStringFromClass([self superclass]), [super version]);
41 - (id)initWithHost:(NSString *)_hostName onPort:(unsigned int)_port {
42 if ((self = [super init])) {
44 [[WOHTTPConnection alloc] initWithHost:_hostName onPort:_port];
46 self->rewriteHost = YES;
52 return [self initWithHost:nil onPort:0];
56 [self->logFilePrefix release];
57 [self->client release];
63 - (void)enableRawLogging {
64 [self logWithFormat:@"enabling raw logging ..."];
65 self->rawLogRequest = YES;
66 self->rawLogResponse = YES;
69 - (void)setLogFilePrefix:(NSString *)_p {
70 ASSIGNCOPY(self->logFilePrefix, _p);
75 - (void)logMessage:(WOMessage *)_msg prefix:(NSString *)_p
81 const unsigned char *s;
83 if (self->logFilePrefix) {
86 fn = [self->logFilePrefix stringByAppendingFormat:@"%04i.%@",
88 if ((fh = fopen([fn cString], "w")) == NULL)
89 [self logWithFormat:@"could not open: %@", fn];
93 printf("%s", [_p cString]);
94 if (fh) fprintf(fh, "%s", [_p cString]);
98 keys = [[_msg headerKeys] objectEnumerator];
99 while ((key = [keys nextObject])) {
103 vals = [[_msg headersForKey:key] objectEnumerator];
104 while ((val = [vals nextObject])) {
105 s = [[val stringValue] cString];
106 printf("%s: %s\n", [key cString], s);
107 if (fh) fprintf(fh, "%s: %s\r\n", [key cString], s);
112 if ((s = [[_msg contentAsString] cString])) {
114 if (fh) fprintf(fh, "\r\n%s", s);
118 if (fh) fprintf(fh, "\r\n");
124 - (void)logRequest:(WORequest *)_rq {
126 printf("PROXY REQUEST:---\n");
128 rl = [NSString stringWithFormat:@"%@ %@ %@\r\n",
129 [_rq method], [_rq uri], [_rq httpVersion]];
130 [self logMessage:_rq prefix:rl ext:@"http"];
134 - (void)logResponse:(WOResponse *)_r {
136 printf("PROXY RESPONSE:---\n");
138 rl = [NSString stringWithFormat:@"%@ %i\r\n", [_r httpVersion], [_r status]];
139 [self logMessage:_r prefix:rl ext:@"http-rs"];
145 - (WOResponse *)failedResponse:(NSString *)_txt forRequest:(WORequest *)_rq{
148 r = [WOResponse responseWithRequest:_rq];
150 [r appendContentHTMLString:_txt];
154 - (WORequest *)fixupRequest:(WORequest *)_request {
157 - (WOResponse *)fixupResponse:(WOResponse *)_r {
161 - (WORequest *)makeProxyRequest:(WORequest *)_request url:(NSURL *)_url {
162 /* this is basically a copy with a modified URI ... */
165 rq = [[WORequest alloc]
166 initWithMethod:[_request method]
168 httpVersion:[_request httpVersion]
169 headers:[_request headers]
170 content:[_request content]
171 userInfo:[_request userInfo]];
172 return [rq autorelease];
175 - (WOResponse *)handleRequest:(WORequest *)_request {
176 WOHTTPConnection *targetClient;
180 targetClient = self->client;
182 _request = [self fixupRequest:_request];
184 if ([[_request uri] hasPrefix:@"http://"]) {
187 purl = [NSURL URLWithString:[_request uri]];
188 [self logWithFormat:@"got a proxy request: %@", purl];
190 _request = [self makeProxyRequest:_request url:purl];
192 targetClient = [[WOHTTPConnection alloc] initWithHost:[purl host]
193 onPort:[[purl port] intValue]];
194 targetClient = [targetClient autorelease];
197 if (self->rawLogRequest)
198 [self logRequest:_request];
200 /* force HTTP/1.0 ... */
201 [_request setHTTPVersion:@"HTTP/1.0"];
203 if (![targetClient sendRequest:_request]) {
204 [self logWithFormat:@"forwarding request to client failed."];
205 return [self failedResponse:@"forwarding request to client failed."
206 forRequest:_request];
209 if ((r = [targetClient readResponse]) == nil) {
210 [self logWithFormat:@"reading response from client failed."];
211 return [self failedResponse:@"reading response from client failed."
212 forRequest:_request];
215 r = [self fixupResponse:r];
217 if (self->rawLogResponse)
218 [self logResponse:r];
225 - (NSString *)loggingPrefix {
226 return @"[proxy-handler]";
229 @end /* WOProxyRequestHandler */