2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #import <Foundation/NSObject.h> // required by gstep-base
23 #import <Foundation/NSURLHandle.h>
24 #import <Foundation/NSURL.h>
29 An URLHandle class which uses WO classes (WOHTTPConnection, WORequest, ..)
30 to get/set HTTP resources.
33 @interface WOHTTPURLHandle : NSURLHandle
37 WOResponse *cachedResponse;
38 NSURLHandleStatus status;
42 #include <NGObjWeb/WOHTTPConnection.h>
43 #include <NGObjWeb/WOResponse.h>
44 #include <NGObjWeb/WORequest.h>
47 @implementation WOHTTPURLHandle
49 + (BOOL)canInitWithURL:(NSURL *)_url {
50 return [[_url scheme] isEqualToString:@"http"];
53 - (id)initWithURL:(NSURL *)_url cached:(BOOL)_flag {
54 if (![[_url scheme] isEqualToString:@"http"]) {
55 NSLog(@"%s: invalid URL scheme %@ for WOHTTPURLHandle !",
56 __PRETTY_FUNCTION__, [_url scheme]);
61 self->shallCache = _flag;
62 self->url = [_url copy];
63 self->status = NSURLHandleNotLoaded;
67 RELEASE(self->cachedResponse);
72 - (WOResponse *)_fetchURL:(NSURL *)_url {
73 WOHTTPConnection *connection;
75 WOResponse *response = nil;
77 connection = [[WOHTTPConnection alloc] initWithHost:[_url host]
78 onPort:[[_url port] intValue]];
79 if (connection == nil) {
80 self->status = NSURLHandleLoadFailed;
84 request = [[WORequest alloc] initWithMethod:@"GET"
86 httpVersion:@"HTTP/1.0"
92 self->status = NSURLHandleLoadFailed;
96 if ([connection sendRequest:request]) {
97 if ((response = [connection readResponse])) {
99 ASSIGN(self->cachedResponse, response);
100 self->status = NSURLHandleLoadSucceeded;
104 self->status = NSURLHandleLoadFailed;
107 self->status = NSURLHandleLoadFailed;
113 return AUTORELEASE(response);
116 - (NSData *)loadInForeground {
117 WOResponse *response;
120 response = [self _fetchURL:self->url];
121 data = [response content];
123 return AUTORELEASE(data);
125 - (void)loadInBackground {
126 [self loadInForeground];
129 - (void)flushCachedData {
130 RELEASE(self->cachedResponse);
131 self->cachedResponse = nil;
134 - (NSData *)resourceData {
135 if (self->cachedResponse) {
138 data = [self->cachedResponse content];
140 return AUTORELEASE(data);
143 return [self loadInForeground];
145 - (NSData *)availableResourceData {
148 data = [self->cachedResponse content];
150 return AUTORELEASE(data);
153 - (NSURLHandleStatus)status {
156 - (NSString *)failureReason {
157 if (self->status != NSURLHandleLoadFailed)
160 return @"loading of HTTP URL failed";
165 - (id)propertyForKey:(NSString *)_key {
166 WOResponse *response;
168 if (self->cachedResponse)
169 return [self->cachedResponse headerForKey:_key];
171 response = [self _fetchURL:self->url];
172 return [response headerForKey:_key];
174 - (id)propertyForKeyIfAvailable:(NSString *)_key {
175 return [self->cachedResponse headerForKey:_key];
180 - (BOOL)writeData:(NSData *)__data {
181 WOHTTPConnection *connection;
183 WOResponse *response;
185 [self flushCachedData];
187 connection = [[WOHTTPConnection alloc] initWithHost:[self->url host]
188 onPort:[[self->url port] intValue]];
189 if (connection == nil)
192 request = [[WORequest alloc] initWithMethod:@"PUT"
194 httpVersion:@"HTTP/1.0"
198 if (request == nil) {
203 if ([connection sendRequest:request])
204 response = [connection readResponse];
209 if ([response status] != 200)
216 return response ? YES : NO;
219 @end /* WOHTTPURLHandle */