GCDWebServerConnection.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2012-2015, Pierre-Olivier Latour
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * The name of Pierre-Olivier Latour may not be used to endorse
  12. or promote products derived from this software without specific
  13. prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import "GCDWebServer.h"
  26. @class GCDWebServerHandler;
  27. /**
  28. * The GCDWebServerConnection class is instantiated by GCDWebServer to handle
  29. * each new HTTP connection. Each instance stays alive until the connection is
  30. * closed.
  31. *
  32. * You cannot use this class directly, but it is made public so you can
  33. * subclass it to override some hooks. Use the GCDWebServerOption_ConnectionClass
  34. * option for GCDWebServer to install your custom subclass.
  35. *
  36. * @warning The GCDWebServerConnection retains the GCDWebServer until the
  37. * connection is closed.
  38. */
  39. @interface GCDWebServerConnection : NSObject
  40. /**
  41. * Returns the GCDWebServer that owns the connection.
  42. */
  43. @property(nonatomic, readonly) GCDWebServer* server;
  44. /**
  45. * Returns YES if the connection is using IPv6.
  46. */
  47. @property(nonatomic, readonly, getter=isUsingIPv6) BOOL usingIPv6;
  48. /**
  49. * Returns the address of the local peer (i.e. server) of the connection
  50. * as a raw "struct sockaddr".
  51. */
  52. @property(nonatomic, readonly) NSData* localAddressData;
  53. /**
  54. * Returns the address of the local peer (i.e. server) of the connection
  55. * as a string.
  56. */
  57. @property(nonatomic, readonly) NSString* localAddressString;
  58. /**
  59. * Returns the address of the remote peer (i.e. client) of the connection
  60. * as a raw "struct sockaddr".
  61. */
  62. @property(nonatomic, readonly) NSData* remoteAddressData;
  63. /**
  64. * Returns the address of the remote peer (i.e. client) of the connection
  65. * as a string.
  66. */
  67. @property(nonatomic, readonly) NSString* remoteAddressString;
  68. /**
  69. * Returns the total number of bytes received from the remote peer (i.e. client)
  70. * so far.
  71. */
  72. @property(nonatomic, readonly) NSUInteger totalBytesRead;
  73. /**
  74. * Returns the total number of bytes sent to the remote peer (i.e. client) so far.
  75. */
  76. @property(nonatomic, readonly) NSUInteger totalBytesWritten;
  77. @end
  78. /**
  79. * Hooks to customize the behavior of GCDWebServer HTTP connections.
  80. *
  81. * @warning These methods can be called on any GCD thread.
  82. * Be sure to also call "super" when overriding them.
  83. */
  84. @interface GCDWebServerConnection (Subclassing)
  85. /**
  86. * This method is called when the connection is opened.
  87. *
  88. * Return NO to reject the connection e.g. after validating the local
  89. * or remote address.
  90. */
  91. - (BOOL)open;
  92. /**
  93. * This method is called whenever data has been received
  94. * from the remote peer (i.e. client).
  95. *
  96. * @warning Do not attempt to modify this data.
  97. */
  98. - (void)didReadBytes:(const void*)bytes length:(NSUInteger)length;
  99. /**
  100. * This method is called whenever data has been sent
  101. * to the remote peer (i.e. client).
  102. *
  103. * @warning Do not attempt to modify this data.
  104. */
  105. - (void)didWriteBytes:(const void*)bytes length:(NSUInteger)length;
  106. /**
  107. * This method is called after the HTTP headers have been received to
  108. * allow replacing the request URL by another one.
  109. *
  110. * The default implementation returns the original URL.
  111. */
  112. - (NSURL*)rewriteRequestURL:(NSURL*)url withMethod:(NSString*)method headers:(NSDictionary*)headers;
  113. /**
  114. * Assuming a valid HTTP request was received, this method is called before
  115. * the request is processed.
  116. *
  117. * Return a non-nil GCDWebServerResponse to bypass the request processing entirely.
  118. *
  119. * The default implementation checks for HTTP authentication if applicable
  120. * and returns a barebone 401 status code response if authentication failed.
  121. */
  122. - (GCDWebServerResponse*)preflightRequest:(GCDWebServerRequest*)request;
  123. /**
  124. * Assuming a valid HTTP request was received and -preflightRequest: returned nil,
  125. * this method is called to process the request by executing the handler's
  126. * process block.
  127. */
  128. - (void)processRequest:(GCDWebServerRequest*)request completion:(GCDWebServerCompletionBlock)completion;
  129. /**
  130. * Assuming a valid HTTP request was received and either -preflightRequest:
  131. * or -processRequest:completion: returned a non-nil GCDWebServerResponse,
  132. * this method is called to override the response.
  133. *
  134. * You can either modify the current response and return it, or return a
  135. * completely new one.
  136. *
  137. * The default implementation replaces any response matching the "ETag" or
  138. * "Last-Modified-Date" header of the request by a barebone "Not-Modified" (304)
  139. * one.
  140. */
  141. - (GCDWebServerResponse*)overrideResponse:(GCDWebServerResponse*)response forRequest:(GCDWebServerRequest*)request;
  142. /**
  143. * This method is called if any error happens while validing or processing
  144. * the request or if no GCDWebServerResponse was generated during processing.
  145. *
  146. * @warning If the request was invalid (e.g. the HTTP headers were malformed),
  147. * the "request" argument will be nil.
  148. */
  149. - (void)abortRequest:(GCDWebServerRequest*)request withStatusCode:(NSInteger)statusCode;
  150. /**
  151. * Called when the connection is closed.
  152. */
  153. - (void)close;
  154. @end