PageRenderTime 14ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/HTTPFetcher/Tests/GTMHTTPServer.h

http://macfuse.googlecode.com/
C++ Header | 145 lines | 69 code | 22 blank | 54 comment | 0 complexity | 11f70e9e304bc04bb347a731bc9464ee MD5 | raw file
  1/* Copyright (c) 2010 Google Inc.
  2 *
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 *
  7 *     http://www.apache.org/licenses/LICENSE-2.0
  8 *
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS,
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License.
 14 */
 15
 16//
 17//  GTMHTTPServer.h
 18//
 19//  This is a *very* *simple* webserver that can be built into something, it is
 20//  not meant to stand up a site, it sends all requests to its delegate for
 21//  processing on the main thread.  It does not support pipelining, etc.  It's
 22//  great for places where you need a simple webserver to unittest some code
 23//  that hits a server.
 24//
 25//  NOTE: there are several TODOs left in here as markers for things that could
 26//  be done if one wanted to add more to this class.
 27//
 28//  Based a little on HTTPServer, part of the CocoaHTTPServer sample code
 29//  http://developer.apple.com/samplecode/CocoaHTTPServer/index.html
 30//
 31
 32#import <Foundation/Foundation.h>
 33
 34#if GTM_IPHONE_SDK
 35  #import <CFNetwork/CFNetwork.h>
 36#endif // GTM_IPHONE_SDK
 37
 38// Global contants needed for errors from start
 39
 40#undef _EXTERN
 41#undef _INITIALIZE_AS
 42#ifdef GTMHTTPSERVER_DEFINE_GLOBALS
 43  #define _EXTERN
 44  #define _INITIALIZE_AS(x) =x
 45#else
 46  #define _EXTERN extern
 47  #define _INITIALIZE_AS(x)
 48#endif
 49
 50_EXTERN NSString* const kGTMHTTPServerErrorDomain _INITIALIZE_AS(@"com.google.mactoolbox.HTTPServerDomain");
 51enum {
 52  kGTMHTTPServerSocketCreateFailedError = -100,
 53  kGTMHTTPServerBindFailedError         = -101,
 54  kGTMHTTPServerListenFailedError       = -102,
 55  kGTMHTTPServerHandleCreateFailedError = -103,
 56};
 57
 58@class GTMHTTPRequestMessage, GTMHTTPResponseMessage;
 59
 60// ----------------------------------------------------------------------------
 61
 62// See comment at top of file for the intened use of this class.
 63@interface GTMHTTPServer : NSObject {
 64 @private
 65  id delegate_; // WEAK
 66  uint16_t port_;
 67  BOOL reusePort_;
 68  BOOL localhostOnly_;
 69  NSFileHandle *listenHandle_;
 70  NSMutableArray *connections_;
 71}
 72
 73// The delegate must support the httpServer:handleRequest: method in
 74// NSObject(GTMHTTPServerDelegateMethods) below.
 75- (id)initWithDelegate:(id)delegate;
 76
 77- (id)delegate;
 78
 79// Passing port zero will let one get assigned.
 80- (uint16_t)port;
 81- (void)setPort:(uint16_t)port;
 82
 83// Controls listening socket behavior: SO_REUSEADDR vs SO_REUSEPORT.
 84// The default is NO (SO_REUSEADDR)
 85- (BOOL)reusePort;
 86- (void)setReusePort:(BOOL)reusePort;
 87
 88// Receive connections on the localHost loopback address only or on all
 89// interfaces for this machine.  The default is to only listen on localhost.
 90- (BOOL)localhostOnly;
 91- (void)setLocalhostOnly:(BOOL)yesno;
 92
 93// Start/Stop the web server.  If there is an error starting up the server, |NO|
 94// is returned, and the specific startup failure can be returned in |error| (see
 95// above for the error domain and error codes).  If the server is started, |YES|
 96// is returned and the server's delegate is called for any requests that come
 97// in.
 98- (BOOL)start:(NSError **)error;
 99- (void)stop;
100
101// returns the number of requests currently active in the server (i.e.-being
102// read in, sent replies).
103- (NSUInteger)activeRequestCount;
104
105@end
106
107@interface NSObject (GTMHTTPServerDelegateMethods)
108- (GTMHTTPResponseMessage *)httpServer:(GTMHTTPServer *)server
109                         handleRequest:(GTMHTTPRequestMessage *)request;
110@end
111
112// ----------------------------------------------------------------------------
113
114// Encapsulates an http request, one of these is sent to the server's delegate
115// for each request.
116@interface GTMHTTPRequestMessage : NSObject {
117 @private
118  CFHTTPMessageRef message_;
119}
120- (NSString *)version;
121- (NSURL *)URL;
122- (NSString *)method;
123- (NSData *)body;
124- (NSDictionary *)allHeaderFieldValues;
125@end
126
127// ----------------------------------------------------------------------------
128
129// Encapsulates an http response, the server's delegate should return one for
130// each request received.
131@interface GTMHTTPResponseMessage : NSObject {
132 @private
133  CFHTTPMessageRef message_;
134}
135+ (id)responseWithString:(NSString *)plainText;
136+ (id)responseWithHTMLString:(NSString *)htmlString;
137+ (id)responseWithBody:(NSData *)body
138           contentType:(NSString *)contentType
139            statusCode:(int)statusCode;
140+ (id)emptyResponseWithCode:(int)statusCode;
141// TODO: class method for redirections?
142// TODO: add helper for expire/no-cache
143- (void)setValue:(NSString*)value forHeaderField:(NSString*)headerField;
144- (void)setHeaderValuesFromDictionary:(NSDictionary *)dict;
145@end