![]() |
IOS Streaming Browser 1.0
An IOS streaming browser to stream the display to others or to a projector
|
00001 #import <Foundation/Foundation.h> 00002 00003 @class GCDAsyncSocket; 00004 @class HTTPMessage; 00005 @class HTTPServer; 00006 @class WebSocket; 00007 @protocol HTTPResponse; 00008 00009 00010 #define HTTPConnectionDidDieNotification @"HTTPConnectionDidDie" 00011 00012 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00013 #pragma mark - 00014 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00015 00016 @interface HTTPConfig : NSObject 00017 { 00018 00019 /** 00020 The HTTPServer which is handling the connection 00021 **/ 00022 HTTPServer *server; 00023 00024 /** 00025 The document root for the server 00026 **/ 00027 NSString *documentRoot; 00028 00029 /** 00030 The dispatch queue for requests 00031 **/ 00032 dispatch_queue_t queue; 00033 } 00034 00035 00036 /** 00037 Initializes the HTTPconnection with a server and document root 00038 param HTTPServer 00039 param NSString 00040 returns id (self) 00041 **/ 00042 - (id)initWithServer:(HTTPServer *)server documentRoot:(NSString *)documentRoot; 00043 00044 /** 00045 Initializes the HTTPConnection with a server, document root, and 00046 dispatch queue 00047 param HTTPServer 00048 param NSString 00049 param dispatch_queue 00050 returns id (self) 00051 **/ 00052 - (id)initWithServer:(HTTPServer *)server documentRoot:(NSString *)documentRoot queue:(dispatch_queue_t)q; 00053 00054 00055 // Sets the properties for the instance attributes/variables 00056 @property (nonatomic, readonly) HTTPServer *server; 00057 @property (nonatomic, readonly) NSString *documentRoot; 00058 @property (nonatomic, readonly) dispatch_queue_t queue; 00059 00060 @end 00061 00062 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00063 #pragma mark - 00064 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00065 00066 @interface HTTPConnection : NSObject 00067 { 00068 00069 /** 00070 queue with all the connections 00071 **/ 00072 dispatch_queue_t connectionQueue; 00073 00074 /** 00075 Handles each request one at a time in order 00076 **/ 00077 GCDAsyncSocket *asyncSocket; 00078 00079 /** 00080 HTTP server configuration 00081 **/ 00082 HTTPConfig *config; 00083 00084 /** 00085 Flag for whether the connection started 00086 **/ 00087 BOOL started; 00088 00089 /** 00090 The http request from the host 00091 **/ 00092 HTTPMessage *request; 00093 00094 /** 00095 number of header lines 00096 **/ 00097 unsigned int numHeaderLines; 00098 00099 /** 00100 Flag for whether sent response headers to the host 00101 **/ 00102 BOOL sentResponseHeaders; 00103 00104 00105 /** 00106 A nonce is a server-specified string uniquely generated for each 401 response. 00107 **/ 00108 NSString *nonce; 00109 00110 /** 00111 The last nonce 00112 **/ 00113 long lastNC; 00114 00115 /** 00116 The http response sent to the host 00117 **/ 00118 NSObject<HTTPResponse> *httpResponse; 00119 00120 /** 00121 Mutable array for the response ranges 00122 **/ 00123 NSMutableArray *ranges; 00124 00125 /** 00126 Mutable array for the response range headers 00127 **/ 00128 NSMutableArray *ranges_headers; 00129 00130 00131 /** 00132 The response ranges boundary 00133 **/ 00134 NSString *ranges_boundry; 00135 00136 /** 00137 00138 **/ 00139 int rangeIndex; 00140 00141 /** 00142 The length of the http request from the host 00143 **/ 00144 UInt64 requestContentLength; 00145 00146 /** 00147 The number of bytes received from teh host 00148 **/ 00149 UInt64 requestContentLengthReceived; 00150 00151 /** 00152 HTTP response data sizes 00153 **/ 00154 NSMutableArray *responseDataSizes; 00155 } 00156 00157 00158 /** 00159 Returns HTTPConnection 00160 param GCDAsyncSocket 00161 param HTTPConfig 00162 returns id 00163 **/ 00164 - (id)initWithAsyncSocket:(GCDAsyncSocket *)newSocket configuration:(HTTPConfig *)aConfig; 00165 00166 /** 00167 * Starting point for the HTTP connection after it has been fully initialized (including subclasses). 00168 * This method is called by the HTTP server. 00169 **/ 00170 - (void)start; 00171 00172 /** 00173 * This method is called by the HTTPServer if it is asked to stop. 00174 * The server, in turn, invokes stop on each HTTPConnection instance. 00175 **/ 00176 - (void)stop; 00177 00178 /** 00179 * Starting point for the HTTP connection. 00180 **/ 00181 - (void)startConnection; 00182 00183 /** 00184 Returns whether or not the server will accept messages of a given method at a particular URI. 00185 param NSString 00186 param NSString 00187 returns BOOL 00188 **/ 00189 - (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path; 00190 00191 /** 00192 Returns whether or not the server expects a body from the given method. 00193 00194 In other words, should the server expect a content-length header and associated body from this method. 00195 This would be true in the case of a POST, where the client is sending data, or for something like PUT where the client is supposed to be uploading a file. 00196 00197 param NSString 00198 param NSString 00199 returns BOOL 00200 **/ 00201 - (BOOL)expectsRequestBodyFromMethod:(NSString *)method atPath:(NSString *)path; 00202 00203 /** 00204 * Returns whether or not the server is configured to be a secure server. 00205 * In other words, all connections to this server are immediately secured, thus only secure connections are allowed. 00206 * This is the equivalent of having an https server, where it is assumed that all connections must be secure. 00207 * If this is the case, then unsecure connections will not be allowed on this server, and a separate unsecure server 00208 * would need to be run on a separate port in order to support unsecure connections. 00209 * 00210 * Note: In order to support secure connections, the sslIdentityAndCertificates method must be implemented. 00211 **/ 00212 - (BOOL)isSecureServer; 00213 00214 /** 00215 * This method is expected to returns an array appropriate for use in kCFStreamSSLCertificates SSL Settings. 00216 * It should be an array of SecCertificateRefs except for the first element in the array, which is a SecIdentityRef. 00217 **/ 00218 - (NSArray *)sslIdentityAndCertificates; 00219 00220 /** 00221 Returns whether or not the requested resource is password protected. 00222 In this generic implementation, nothing is password protected. 00223 param NSString 00224 returns BOOL 00225 **/ 00226 - (BOOL)isPasswordProtected:(NSString *)path; 00227 00228 /** 00229 * Returns whether or not the authentication challenge should use digest access authentication. 00230 * The alternative is basic authentication. 00231 * 00232 * If at all possible, digest access authentication should be used because it's more secure. 00233 * Basic authentication sends passwords in the clear and should be avoided unless using SSL/TLS. 00234 **/ 00235 - (BOOL)useDigestAccessAuthentication; 00236 00237 /** 00238 Returns the authentication realm. 00239 In this generic implmentation, a default realm is used for the entire server. 00240 returns NSString 00241 **/ 00242 - (NSString *)realm; 00243 00244 /** 00245 Returns the password for the given username. 00246 param NSString 00247 returns NSString 00248 **/ 00249 - (NSString *)passwordForUser:(NSString *)username; 00250 00251 /** 00252 * Parses the given query string. 00253 * 00254 * For example, if the query is "q=John%20Mayer%20Trio&num=50" 00255 * then this method would return the following dictionary: 00256 * { 00257 * q = "John Mayer Trio" 00258 * num = "50" 00259 * } 00260 **/ 00261 - (NSDictionary *)parseParams:(NSString *)query; 00262 00263 /** 00264 * Parses the query variables in the request URI. 00265 * 00266 * For example, if the request URI was "/search.html?q=John%20Mayer%20Trio&num=50" 00267 * then this method would return the following dictionary: 00268 * { 00269 * q = "John Mayer Trio" 00270 * num = "50" 00271 * } 00272 **/ 00273 - (NSDictionary *)parseGetParams; 00274 00275 /** 00276 Returns the URL as a string for the HTTPMessage 00277 **/ 00278 - (NSString *)requestURI; 00279 00280 /** 00281 * Returns an array of possible index pages. 00282 * For example: {"index.html", "index.htm"} 00283 **/ 00284 - (NSArray *)directoryIndexFileNames; 00285 00286 /** 00287 Converts relative URI path into full file-system path. 00288 param NSString 00289 returns NSString 00290 **/ 00291 - (NSString *)filePathForURI:(NSString *)path; 00292 00293 /** 00294 * This method is called to get a response for a request. 00295 * You may return any object that adopts the HTTPResponse protocol. 00296 * The HTTPServer comes with two such classes: HTTPFileResponse and HTTPDataResponse. 00297 * HTTPFileResponse is a wrapper for an NSFileHandle object, and is the preferred way to send a file response. 00298 * HTTPDataResponse is a wrapper for an NSData object, and may be used to send a custom response. 00299 **/ 00300 - (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path; 00301 00302 /** 00303 param NSString 00304 returns WebSocket 00305 **/ 00306 - (WebSocket *)webSocketForURI:(NSString *)path; 00307 00308 /** 00309 This method is called after receiving all HTTP headers, but before reading any of the request body. 00310 param UInt64 00311 **/ 00312 - (void)prepareForBodyWithSize:(UInt64)contentLength; 00313 00314 /** 00315 This method is called to handle data read from a POST / PUT. 00316 The given data is part of the request body. 00317 param NSData 00318 **/ 00319 - (void)processDataChunk:(NSData *)postDataChunk; 00320 00321 /** 00322 Called if the HTML version is other than what is supported 00323 param NSString 00324 **/ 00325 - (void)handleVersionNotSupported:(NSString *)version; 00326 00327 /** 00328 Called if the authentication information was required and absent, or if authentication failed. 00329 **/ 00330 - (void)handleAuthenticationFailed; 00331 00332 /** 00333 Called if we're unable to find the requested resource. 00334 **/ 00335 - (void)handleResourceNotFound; 00336 00337 /** 00338 * Called if we receive some sort of malformed HTTP request. 00339 * The data parameter is the invalid HTTP header line, including CRLF, as read from GCDAsyncSocket. 00340 * The data parameter may also be nil if the request as a whole was invalid, such as a POST with no Content-Length. 00341 **/ 00342 - (void)handleInvalidRequest:(NSData *)data; 00343 00344 /** 00345 * Called if we receive a HTTP request with a method other than GET or HEAD. 00346 **/ 00347 - (void)handleUnknownMethod:(NSString *)method; 00348 00349 /** 00350 This method is called immediately prior to sending the response headers. 00351 This method adds standard header fields, and then converts the response to an NSData object. 00352 param HTTPMessage 00353 returns NSData 00354 **/ 00355 - (NSData *)preprocessResponse:(HTTPMessage *)response; 00356 00357 /** 00358 * This method is called immediately prior to sending the response headers (for an error). 00359 * This method adds standard header fields, and then converts the response to an NSData object. 00360 **/ 00361 - (NSData *)preprocessErrorResponse:(HTTPMessage *)response; 00362 00363 /** 00364 Returns whether the HTTPConnection should die 00365 returns BOOL 00366 **/ 00367 - (BOOL)shouldDie; 00368 00369 /** 00370 Closes the connection 00371 **/ 00372 - (void)die; 00373 00374 @end 00375 00376 00377 @interface HTTPConnection (AsynchronousHTTPResponse) 00378 00379 /** 00380 param NSObject with HTTPResponse protocol 00381 **/ 00382 - (void)responseHasAvailableData:(NSObject<HTTPResponse> *)sender; 00383 00384 /** 00385 param NSObject with HTTPResponse protocl 00386 **/ 00387 - (void)responseDidAbort:(NSObject<HTTPResponse> *)sender; 00388 00389 @end