Parcourir la source

Merge pull request #89 from nickgravelyn/capture-regex-parameters-in-request

Added attributes to GCDWebServerRequest with support to retrieve regex captured variables
Pierre-Olivier Latour il y a 11 ans
Parent
commit
6b15bdaa4e

+ 15 - 2
GCDWebServer/Core/GCDWebServer.m

@@ -793,10 +793,23 @@ static inline NSString* _EncodeBase64(NSString* string) {
       if (![requestMethod isEqualToString:method]) {
       if (![requestMethod isEqualToString:method]) {
         return nil;
         return nil;
       }
       }
-      if ([expression firstMatchInString:urlPath options:0 range:NSMakeRange(0, urlPath.length)] == nil) {
+
+      NSArray* matches = [expression matchesInString:urlPath options:0 range:NSMakeRange(0, urlPath.length)];
+      if (matches.count == 0) {
         return nil;
         return nil;
       }
       }
-      return ARC_AUTORELEASE([[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery]);
+
+      NSMutableArray* captures = [NSMutableArray array];
+      for (NSTextCheckingResult* result in matches) {
+        // Start at 1; index 0 is the whole string
+        for (NSUInteger i = 1; i < result.numberOfRanges; i++) {
+          [captures addObject:[urlPath substringWithRange:[result rangeAtIndex:i]]];
+        }
+      }
+
+      GCDWebServerRequest* request = [[aClass alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
+      [request setAttribute:captures forKey:GCDWebServerRequestAttribute_RegexCaptures];
+      return ARC_AUTORELEASE(request);
       
       
     } processBlock:block];
     } processBlock:block];
   } else {
   } else {

+ 1 - 0
GCDWebServer/Core/GCDWebServerPrivate.h

@@ -153,6 +153,7 @@ extern NSString* GCDWebServerComputeMD5Digest(NSString* format, ...) NS_FORMAT_F
 - (BOOL)performOpen:(NSError**)error;
 - (BOOL)performOpen:(NSError**)error;
 - (BOOL)performWriteData:(NSData*)data error:(NSError**)error;
 - (BOOL)performWriteData:(NSData*)data error:(NSError**)error;
 - (BOOL)performClose:(NSError**)error;
 - (BOOL)performClose:(NSError**)error;
+- (void)setAttribute:(id)attribute forKey:(NSString*)key;
 @end
 @end
 
 
 @interface GCDWebServerResponse ()
 @interface GCDWebServerResponse ()

+ 16 - 0
GCDWebServer/Core/GCDWebServerRequest.h

@@ -27,6 +27,15 @@
 
 
 #import <Foundation/Foundation.h>
 #import <Foundation/Foundation.h>
 
 
+/**
+ *  Attribute key to retrieve an NSArray containing NSStrings from a GCDWebServerRequest
+ *  with the contents of any regular expression captures done on the request path.
+ *
+ *  @warning This attribute will only be set on the request if adding a handler using 
+ *  -addHandlerForMethod:pathRegex:requestClass:processBlock:.
+ */
+extern NSString* const GCDWebServerRequestAttribute_RegexCaptures;
+
 /**
 /**
  *  This protocol is used by the GCDWebServerConnection to communicate with
  *  This protocol is used by the GCDWebServerConnection to communicate with
  *  the GCDWebServerRequest and write the received HTTP body data.
  *  the GCDWebServerRequest and write the received HTTP body data.
@@ -163,4 +172,11 @@
  */
  */
 - (BOOL)hasByteRange;
 - (BOOL)hasByteRange;
 
 
+/**
+ *  Retrieves an attribute associated with this request using the given key.
+ *
+ *  @return The attribute value for the key.
+ */
+- (id)attributeForKey:(NSString*)key;
+
 @end
 @end

+ 13 - 0
GCDWebServer/Core/GCDWebServerRequest.m

@@ -29,6 +29,8 @@
 
 
 #import "GCDWebServerPrivate.h"
 #import "GCDWebServerPrivate.h"
 
 
+NSString* const GCDWebServerRequestAttribute_RegexCaptures = @"GCDWebServerRequestAttribute_RegexCaptures";
+
 #define kZlibErrorDomain @"ZlibErrorDomain"
 #define kZlibErrorDomain @"ZlibErrorDomain"
 #define kGZipInitialBufferSize (256 * 1024)
 #define kGZipInitialBufferSize (256 * 1024)
 
 
@@ -152,6 +154,7 @@
   
   
   BOOL _opened;
   BOOL _opened;
   NSMutableArray* _decoders;
   NSMutableArray* _decoders;
+  NSMutableDictionary* _attributes;
   id<GCDWebServerBodyWriter> __unsafe_unretained _writer;
   id<GCDWebServerBodyWriter> __unsafe_unretained _writer;
 }
 }
 @end
 @end
@@ -238,6 +241,7 @@
     }
     }
     
     
     _decoders = [[NSMutableArray alloc] init];
     _decoders = [[NSMutableArray alloc] init];
+    _attributes = [[NSMutableDictionary alloc] init];
   }
   }
   return self;
   return self;
 }
 }
@@ -252,6 +256,7 @@
   ARC_RELEASE(_modifiedSince);
   ARC_RELEASE(_modifiedSince);
   ARC_RELEASE(_noneMatch);
   ARC_RELEASE(_noneMatch);
   ARC_RELEASE(_decoders);
   ARC_RELEASE(_decoders);
+  ARC_RELEASE(_attributes);
   
   
   ARC_DEALLOC(super);
   ARC_DEALLOC(super);
 }
 }
@@ -264,6 +269,10 @@
   return GCDWebServerIsValidByteRange(_range);
   return GCDWebServerIsValidByteRange(_range);
 }
 }
 
 
+- (id)attributeForKey:(NSString*)key {
+  return [_attributes objectForKey:key];
+}
+
 - (BOOL)open:(NSError**)error {
 - (BOOL)open:(NSError**)error {
   return YES;
   return YES;
 }
 }
@@ -307,6 +316,10 @@
   return [_writer close:error];
   return [_writer close:error];
 }
 }
 
 
+- (void)setAttribute:(id)attribute forKey:(NSString*)key {
+  [_attributes setValue:attribute forKey:key];
+}
+
 - (NSString*)description {
 - (NSString*)description {
   NSMutableString* description = [NSMutableString stringWithFormat:@"%@ %@", _method, _path];
   NSMutableString* description = [NSMutableString stringWithFormat:@"%@ %@", _method, _path];
   for (NSString* argument in [[_query allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
   for (NSString* argument in [[_query allKeys] sortedArrayUsingSelector:@selector(compare:)]) {