Skip to content

Commit 79d6075

Browse files
committed
Add remote and local addresses to GCDWebServerRequest
1 parent fe472cd commit 79d6075

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

GCDWebServer/Core/GCDWebServerConnection.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ - (void)_readRequestHeaders {
548548
}
549549
}
550550
if (_request) {
551+
_request.localAddressData = self.localAddressData;
552+
_request.remoteAddressData = self.remoteAddressData;
551553
if ([_request hasBody]) {
552554
[_request prepareForWriting];
553555
if (_request.usesChunkedTransferEncoding || (extraData.length <= _request.contentLength)) {

GCDWebServer/Core/GCDWebServerPrivate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ extern NSString* GCDWebServerStringFromSockAddr(const struct sockaddr* addr, BOO
211211

212212
@interface GCDWebServerRequest ()
213213
@property(nonatomic, readonly) BOOL usesChunkedTransferEncoding;
214+
@property(nonatomic, readwrite) NSData* localAddressData;
215+
@property(nonatomic, readwrite) NSData* remoteAddressData;
214216
- (void)prepareForWriting;
215217
- (BOOL)performOpen:(NSError**)error;
216218
- (BOOL)performWriteData:(NSData*)data error:(NSError**)error;

GCDWebServer/Core/GCDWebServerRequest.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@ extern NSString* const GCDWebServerRequestAttribute_RegexCaptures;
157157
*/
158158
@property(nonatomic, readonly) BOOL acceptsGzipContentEncoding;
159159

160+
/**
161+
* Returns the address of the local peer (i.e. server) for the request
162+
* as a raw "struct sockaddr".
163+
*/
164+
@property(nonatomic, readonly) NSData* localAddressData;
165+
166+
/**
167+
* Returns the address of the local peer (i.e. server) for the request
168+
* as a string.
169+
*/
170+
@property(nonatomic, readonly) NSString* localAddressString;
171+
172+
/**
173+
* Returns the address of the remote peer (i.e. client) for the request
174+
* as a raw "struct sockaddr".
175+
*/
176+
@property(nonatomic, readonly) NSData* remoteAddressData;
177+
178+
/**
179+
* Returns the address of the remote peer (i.e. client) for the request
180+
* as a string.
181+
*/
182+
@property(nonatomic, readonly) NSString* remoteAddressString;
183+
160184
/**
161185
* This method is the designated initializer for the class.
162186
*/

GCDWebServer/Core/GCDWebServerRequest.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ @interface GCDWebServerRequest () {
157157
NSString* _noneMatch;
158158
NSRange _range;
159159
BOOL _gzipAccepted;
160+
NSData* _localAddress;
161+
NSData* _remoteAddress;
160162

161163
BOOL _opened;
162164
NSMutableArray* _decoders;
@@ -168,7 +170,7 @@ @interface GCDWebServerRequest () {
168170
@implementation GCDWebServerRequest : NSObject
169171

170172
@synthesize method=_method, URL=_url, headers=_headers, path=_path, query=_query, contentType=_type, contentLength=_length, ifModifiedSince=_modifiedSince, ifNoneMatch=_noneMatch,
171-
byteRange=_range, acceptsGzipContentEncoding=_gzipAccepted, usesChunkedTransferEncoding=_chunked;
173+
byteRange=_range, acceptsGzipContentEncoding=_gzipAccepted, usesChunkedTransferEncoding=_chunked, localAddressData=_localAddress, remoteAddressData=_remoteAddress;
172174

173175
- (instancetype)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query {
174176
if ((self = [super init])) {
@@ -308,6 +310,14 @@ - (void)setAttribute:(id)attribute forKey:(NSString*)key {
308310
[_attributes setValue:attribute forKey:key];
309311
}
310312

313+
- (NSString*)localAddressString {
314+
return GCDWebServerStringFromSockAddr(_localAddress.bytes, YES);
315+
}
316+
317+
- (NSString*)remoteAddressString {
318+
return GCDWebServerStringFromSockAddr(_remoteAddress.bytes, YES);
319+
}
320+
311321
- (NSString*)description {
312322
NSMutableString* description = [NSMutableString stringWithFormat:@"%@ %@", _method, _path];
313323
for (NSString* argument in [[_query allKeys] sortedArrayUsingSelector:@selector(compare:)]) {

0 commit comments

Comments
 (0)