]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOHTTPURLHandle.m
removed bogus line
[sope] / sope-appserver / NGObjWeb / WOHTTPURLHandle.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #import <Foundation/NSObject.h> // required by gstep-base
24 #import <Foundation/NSURLHandle.h>
25 #import <Foundation/NSURL.h>
26
27 @class WOResponse;
28
29 /*
30   An URLHandle class which uses WO classes (WOHTTPConnection, WORequest, ..)
31   to get/set HTTP resources.
32 */
33
34 @interface WOHTTPURLHandle : NSURLHandle
35 {
36   NSURL             *url;
37   BOOL              shallCache;
38   WOResponse        *cachedResponse;
39   NSURLHandleStatus status;
40 }
41 @end
42
43 #include <NGObjWeb/WOHTTPConnection.h>
44 #include <NGObjWeb/WOResponse.h>
45 #include <NGObjWeb/WORequest.h>
46 #include "common.h"
47
48 @implementation WOHTTPURLHandle
49
50 + (BOOL)canInitWithURL:(NSURL *)_url {
51   return [[_url scheme] isEqualToString:@"http"];
52 }
53
54 - (id)initWithURL:(NSURL *)_url cached:(BOOL)_flag {
55   if (![[_url scheme] isEqualToString:@"http"]) {
56     NSLog(@"%s: invalid URL scheme %@ for WOHTTPURLHandle !",
57           __PRETTY_FUNCTION__, [_url scheme]);
58     RELEASE(self);
59     return nil;
60   }
61   
62   self->shallCache = _flag;
63   self->url        = [_url copy];
64   self->status     = NSURLHandleNotLoaded;
65   return self;
66 }
67 - (void)dealloc {
68   RELEASE(self->cachedResponse);
69   RELEASE(self->url);
70   [super dealloc];
71 }
72
73 - (WOResponse *)_fetchURL:(NSURL *)_url {
74   WOHTTPConnection *connection;
75   WORequest        *request;
76   WOResponse       *response = nil;
77   
78   connection = [[WOHTTPConnection alloc] initWithHost:[_url host]
79                                          onPort:[[_url port] intValue]];
80   if (connection == nil) {
81     self->status = NSURLHandleLoadFailed;
82     return nil;
83   }
84
85   request = [[WORequest alloc] initWithMethod:@"GET"
86                                uri:[_url path]
87                                httpVersion:@"HTTP/1.0"
88                                headers:nil
89                                content:nil
90                                userInfo:nil];
91   if (request == nil) {
92     RELEASE(connection);
93     self->status = NSURLHandleLoadFailed;
94     return nil;
95   }
96   
97   if ([connection sendRequest:request]) {
98     if ((response = [connection readResponse])) {
99       if (self->shallCache)
100         ASSIGN(self->cachedResponse, response);
101       self->status = NSURLHandleLoadSucceeded;
102       RETAIN(response);
103     }
104     else
105       self->status = NSURLHandleLoadFailed;
106   }
107   else {
108     self->status = NSURLHandleLoadFailed;
109   }
110   
111   RELEASE(request);
112   RELEASE(connection);
113   
114   return AUTORELEASE(response);
115 }
116
117 - (NSData *)loadInForeground {
118   WOResponse *response;
119   NSData     *data;
120   
121   response = [self _fetchURL:self->url];
122   data = [response content];
123   RETAIN(data);
124   return AUTORELEASE(data);
125 }
126 - (void)loadInBackground {
127   [self loadInForeground];
128 }
129
130 - (void)flushCachedData {
131   RELEASE(self->cachedResponse);
132   self->cachedResponse = nil;
133 }
134
135 - (NSData *)resourceData {
136   if (self->cachedResponse) {
137     NSData *data;
138
139     data = [self->cachedResponse content];
140     RETAIN(data);
141     return AUTORELEASE(data);
142   }
143
144   return [self loadInForeground];
145 }
146 - (NSData *)availableResourceData {
147   NSData *data;
148
149   data = [self->cachedResponse content];
150   RETAIN(data);
151   return AUTORELEASE(data);
152 }
153
154 - (NSURLHandleStatus)status {
155   return self->status;
156 }
157 - (NSString *)failureReason {
158   if (self->status != NSURLHandleLoadFailed)
159     return nil;
160
161   return @"loading of HTTP URL failed";
162 }
163
164 /* properties */
165
166 - (id)propertyForKey:(NSString *)_key {
167   WOResponse *response;
168   
169   if (self->cachedResponse)
170     return [self->cachedResponse headerForKey:_key];
171   
172   response = [self _fetchURL:self->url];
173   return [response headerForKey:_key];
174 }
175 - (id)propertyForKeyIfAvailable:(NSString *)_key {
176   return [self->cachedResponse headerForKey:_key];
177 }
178
179 /* writing */
180
181 - (BOOL)writeData:(NSData *)__data {
182   WOHTTPConnection *connection;
183   WORequest        *request;
184   WOResponse       *response;
185   
186   [self flushCachedData];
187   
188   connection = [[WOHTTPConnection alloc] initWithHost:[self->url host]
189                                          onPort:[[self->url port] intValue]];
190   if (connection == nil)
191     return NO;
192   
193   request = [[WORequest alloc] initWithMethod:@"PUT"
194                                uri:[self->url path]
195                                httpVersion:@"HTTP/1.0"
196                                headers:nil
197                                content:__data
198                                userInfo:nil];
199   if (request == nil) {
200     RELEASE(connection);
201     return NO;
202   }
203   
204   if ([connection sendRequest:request])
205     response = [connection readResponse];
206   else
207     response = nil;
208   
209   if (response) {
210     if ([response status] != 200)
211       response = nil;
212   }
213   
214   RELEASE(request);
215   RELEASE(connection);
216   
217   return response ? YES : NO;
218 }
219
220 @end /* WOHTTPURLHandle */