/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. // GTMHTTPServer.h
  17. //
  18. // This is a *very* *simple* webserver that can be built into something, it is
  19. // not meant to stand up a site, it sends all requests to its delegate for
  20. // processing on the main thread. It does not support pipelining, etc. It's
  21. // great for places where you need a simple webserver to unittest some code
  22. // that hits a server.
  23. //
  24. // NOTE: there are several TODOs left in here as markers for things that could
  25. // be done if one wanted to add more to this class.
  26. //
  27. // Based a little on HTTPServer, part of the CocoaHTTPServer sample code
  28. // http://developer.apple.com/samplecode/CocoaHTTPServer/index.html
  29. //
  30. #import <Foundation/Foundation.h>
  31. #if GTM_IPHONE_SDK
  32. #import <CFNetwork/CFNetwork.h>
  33. #endif // GTM_IPHONE_SDK
  34. // Global contants needed for errors from start
  35. #undef _EXTERN
  36. #undef _INITIALIZE_AS
  37. #ifdef GTMHTTPSERVER_DEFINE_GLOBALS
  38. #define _EXTERN
  39. #define _INITIALIZE_AS(x) =x
  40. #else
  41. #define _EXTERN extern
  42. #define _INITIALIZE_AS(x)
  43. #endif
  44. _EXTERN NSString* const kGTMHTTPServerErrorDomain _INITIALIZE_AS(@"com.google.mactoolbox.HTTPServerDomain");
  45. enum {
  46. kGTMHTTPServerSocketCreateFailedError = -100,
  47. kGTMHTTPServerBindFailedError = -101,
  48. kGTMHTTPServerListenFailedError = -102,
  49. kGTMHTTPServerHandleCreateFailedError = -103,
  50. };
  51. @class GTMHTTPRequestMessage, GTMHTTPResponseMessage;
  52. // ----------------------------------------------------------------------------
  53. // See comment at top of file for the intened use of this class.
  54. @interface GTMHTTPServer : NSObject {
  55. @private
  56. id delegate_; // WEAK
  57. uint16_t port_;
  58. BOOL reusePort_;
  59. BOOL localhostOnly_;
  60. NSFileHandle *listenHandle_;
  61. NSMutableArray *connections_;
  62. }
  63. // The delegate must support the httpServer:handleRequest: method in
  64. // NSObject(GTMHTTPServerDelegateMethods) below.
  65. - (id)initWithDelegate:(id)delegate;
  66. - (id)delegate;
  67. // Passing port zero will let one get assigned.
  68. - (uint16_t)port;
  69. - (void)setPort:(uint16_t)port;
  70. // Controls listening socket behavior: SO_REUSEADDR vs SO_REUSEPORT.
  71. // The default is NO (SO_REUSEADDR)
  72. - (BOOL)reusePort;
  73. - (void)setReusePort:(BOOL)reusePort;
  74. // Receive connections on the localHost loopback address only or on all
  75. // interfaces for this machine. The default is to only listen on localhost.
  76. - (BOOL)localhostOnly;
  77. - (void)setLocalhostOnly:(BOOL)yesno;
  78. // Start/Stop the web server. If there is an error starting up the server, |NO|
  79. // is returned, and the specific startup failure can be returned in |error| (see
  80. // above for the error domain and error codes). If the server is started, |YES|
  81. // is returned and the server's delegate is called for any requests that come
  82. // in.
  83. - (BOOL)start:(NSError **)error;
  84. - (void)stop;
  85. // returns the number of requests currently active in the server (i.e.-being
  86. // read in, sent replies).
  87. - (NSUInteger)activeRequestCount;
  88. @end
  89. @interface NSObject (GTMHTTPServerDelegateMethods)
  90. - (GTMHTTPResponseMessage *)httpServer:(GTMHTTPServer *)server
  91. handleRequest:(GTMHTTPRequestMessage *)request;
  92. @end
  93. // ----------------------------------------------------------------------------
  94. // Encapsulates an http request, one of these is sent to the server's delegate
  95. // for each request.
  96. @interface GTMHTTPRequestMessage : NSObject {
  97. @private
  98. CFHTTPMessageRef message_;
  99. }
  100. - (NSString *)version;
  101. - (NSURL *)URL;
  102. - (NSString *)method;
  103. - (NSData *)body;
  104. - (NSDictionary *)allHeaderFieldValues;
  105. @end
  106. // ----------------------------------------------------------------------------
  107. // Encapsulates an http response, the server's delegate should return one for
  108. // each request received.
  109. @interface GTMHTTPResponseMessage : NSObject {
  110. @private
  111. CFHTTPMessageRef message_;
  112. }
  113. + (id)responseWithString:(NSString *)plainText;
  114. + (id)responseWithHTMLString:(NSString *)htmlString;
  115. + (id)responseWithBody:(NSData *)body
  116. contentType:(NSString *)contentType
  117. statusCode:(int)statusCode;
  118. + (id)emptyResponseWithCode:(int)statusCode;
  119. // TODO: class method for redirections?
  120. // TODO: add helper for expire/no-cache
  121. - (void)setValue:(NSString*)value forHeaderField:(NSString*)headerField;
  122. - (void)setHeaderValuesFromDictionary:(NSDictionary *)dict;
  123. @end