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 #import <Foundation/NSObject.h> // required by gstep-base
24 #import <Foundation/NSURLHandle.h>
25 #import <Foundation/NSURL.h>
30 An URLHandle class which uses WO classes (WOHTTPConnection, WORequest, ..)
31 to get/set HTTP resources.
34 @interface WOHTTPURLHandle : NSURLHandle
38 WOResponse *cachedResponse;
39 NSURLHandleStatus status;
43 #include <NGObjWeb/WOHTTPConnection.h>
44 #include <NGObjWeb/WOResponse.h>
45 #include <NGObjWeb/WORequest.h>
48 @implementation WOHTTPURLHandle
50 + (BOOL)canInitWithURL:(NSURL *)_url {
51 return [[_url scheme] isEqualToString:@"http"];
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]);
62 self->shallCache = _flag;
63 self->url = [_url copy];
64 self->status = NSURLHandleNotLoaded;
68 RELEASE(self->cachedResponse);
73 - (WOResponse *)_fetchURL:(NSURL *)_url {
74 WOHTTPConnection *connection;
76 WOResponse *response = nil;
78 connection = [[WOHTTPConnection alloc] initWithHost:[_url host]
79 onPort:[[_url port] intValue]];
80 if (connection == nil) {
81 self->status = NSURLHandleLoadFailed;
85 request = [[WORequest alloc] initWithMethod:@"GET"
87 httpVersion:@"HTTP/1.0"
93 self->status = NSURLHandleLoadFailed;
97 if ([connection sendRequest:request]) {
98 if ((response = [connection readResponse])) {
100 ASSIGN(self->cachedResponse, response);
101 self->status = NSURLHandleLoadSucceeded;
105 self->status = NSURLHandleLoadFailed;
108 self->status = NSURLHandleLoadFailed;
114 return AUTORELEASE(response);
117 - (NSData *)loadInForeground {
118 WOResponse *response;
121 response = [self _fetchURL:self->url];
122 data = [response content];
124 return AUTORELEASE(data);
126 - (void)loadInBackground {
127 [self loadInForeground];
130 - (void)flushCachedData {
131 RELEASE(self->cachedResponse);
132 self->cachedResponse = nil;
135 - (NSData *)resourceData {
136 if (self->cachedResponse) {
139 data = [self->cachedResponse content];
141 return AUTORELEASE(data);
144 return [self loadInForeground];
146 - (NSData *)availableResourceData {
149 data = [self->cachedResponse content];
151 return AUTORELEASE(data);
154 - (NSURLHandleStatus)status {
157 - (NSString *)failureReason {
158 if (self->status != NSURLHandleLoadFailed)
161 return @"loading of HTTP URL failed";
166 - (id)propertyForKey:(NSString *)_key {
167 WOResponse *response;
169 if (self->cachedResponse)
170 return [self->cachedResponse headerForKey:_key];
172 response = [self _fetchURL:self->url];
173 return [response headerForKey:_key];
175 - (id)propertyForKeyIfAvailable:(NSString *)_key {
176 return [self->cachedResponse headerForKey:_key];
181 - (BOOL)writeData:(NSData *)__data {
182 WOHTTPConnection *connection;
184 WOResponse *response;
186 [self flushCachedData];
188 connection = [[WOHTTPConnection alloc] initWithHost:[self->url host]
189 onPort:[[self->url port] intValue]];
190 if (connection == nil)
193 request = [[WORequest alloc] initWithMethod:@"PUT"
195 httpVersion:@"HTTP/1.0"
199 if (request == nil) {
204 if ([connection sendRequest:request])
205 response = [connection readResponse];
210 if ([response status] != 200)
217 return response ? YES : NO;
220 @end /* WOHTTPURLHandle */