-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
558 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
SQDebug/AFNetworking/AFNetworking-4.0.1-Debug/AFNetworking-Debug/SQQueryStringPair.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// SQQueryStringPair.h | ||
// AFNetworking-Debug | ||
// | ||
// Created by 朱双泉 on 2020/11/22. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface SQQueryStringPair : NSObject | ||
|
||
@property (readwrite, nonatomic, strong) id field; | ||
@property (readwrite, nonatomic, strong) id value; | ||
|
||
- (instancetype)initWithField:(id)field value:(id)value; | ||
|
||
- (NSString *)URLEncodedStringValue; | ||
|
||
@end |
59 changes: 59 additions & 0 deletions
59
SQDebug/AFNetworking/AFNetworking-4.0.1-Debug/AFNetworking-Debug/SQQueryStringPair.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// SQQueryStringPair.m | ||
// AFNetworking-Debug | ||
// | ||
// Created by 朱双泉 on 2020/11/22. | ||
// | ||
|
||
#import "SQQueryStringPair.h" | ||
|
||
@implementation SQQueryStringPair | ||
|
||
NSString * SQPercentEscapedStringFromString(NSString *string) { | ||
// return string; | ||
static NSString * const kSQCharactersGeneralDelimitersToEncode = @":#[]@"; | ||
static NSString * const kSQCharactersSubDelimitersToEncode = @"!$&'()*+,;="; | ||
NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy]; | ||
[allowedCharacterSet removeCharactersInString:[kSQCharactersGeneralDelimitersToEncode stringByAppendingString:kSQCharactersSubDelimitersToEncode]]; | ||
NSString *encoded = [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet]; | ||
|
||
static NSUInteger const batchSize = 50; | ||
NSUInteger index = 0; | ||
NSMutableString *escaped = @"".mutableCopy; | ||
while (index < string.length) { | ||
NSUInteger length = MIN(string.length - index, batchSize); | ||
NSRange range = NSMakeRange(index, length); | ||
range = [string rangeOfComposedCharacterSequencesForRange:range]; | ||
NSString *substring = [string substringWithRange:range]; | ||
NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet]; | ||
[escaped appendString:encoded]; | ||
index += range.length; | ||
} | ||
return encoded; | ||
} | ||
|
||
- (instancetype)initWithField:(id)field value:(id)value { | ||
self = [super init]; | ||
if (!self) { | ||
return nil; | ||
} | ||
|
||
self.field = field; | ||
self.value = value; | ||
|
||
return self; | ||
} | ||
|
||
- (NSString *)URLEncodedStringValue { | ||
if (!self.value || [self.value isEqual:[NSNull null]]) { | ||
return SQPercentEscapedStringFromString([self.field description]); | ||
} else { | ||
return [NSString stringWithFormat:@"%@=%@", SQPercentEscapedStringFromString([self.field description]), SQPercentEscapedStringFromString([self.value description])]; | ||
} | ||
} | ||
|
||
- (NSString *)description { | ||
return [self URLEncodedStringValue]; | ||
} | ||
|
||
@end |
18 changes: 18 additions & 0 deletions
18
SQDebug/AFNetworking/AFNetworking-4.0.1-Debug/AFNetworking-Debug/SQURLRequestSerialization.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// SQURLRequestSerialization.h | ||
// AFNetworking-Debug | ||
// | ||
// Created by 朱双泉 on 2020/11/22. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface SQURLRequestSerialization : NSObject | ||
|
||
NSString * SQQueryStringFromParameters(NSDictionary *parameters); | ||
|
||
NSArray * SQQueryStringPairFromDictionary(NSDictionary *dictionary); | ||
|
||
NSArray * SQQueryStringPairsFromKeyAndValue(NSString *key, id value); | ||
|
||
@end |
52 changes: 52 additions & 0 deletions
52
SQDebug/AFNetworking/AFNetworking-4.0.1-Debug/AFNetworking-Debug/SQURLRequestSerialization.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// SQURLRequestSerialization.m | ||
// AFNetworking-Debug | ||
// | ||
// Created by 朱双泉 on 2020/11/22. | ||
// | ||
|
||
#import "SQURLRequestSerialization.h" | ||
#import "SQQueryStringPair.h" | ||
|
||
@implementation SQURLRequestSerialization | ||
|
||
NSString * SQQueryStringFromParameters(NSDictionary *parameters) { | ||
NSMutableArray *mutablePairs = [NSMutableArray array]; | ||
for (SQQueryStringPair *pair in SQQueryStringPairFromDictionary(parameters)) { | ||
[mutablePairs addObject:[pair URLEncodedStringValue]]; | ||
} | ||
return [mutablePairs componentsJoinedByString:@"&"]; | ||
} | ||
|
||
NSArray * SQQueryStringPairFromDictionary(NSDictionary *dictionary) { | ||
return SQQueryStringPairsFromKeyAndValue(nil, dictionary); | ||
} | ||
|
||
NSArray * SQQueryStringPairsFromKeyAndValue(NSString *key, id value) { | ||
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array]; | ||
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(compare:)]; | ||
if ([value isKindOfClass:[NSDictionary class]]) { | ||
NSDictionary *dictionary = value; | ||
for (id nestedKey in [dictionary.allKeys sortedArrayUsingDescriptors:@[ sortDescriptor ]]) { | ||
id nestedValue = dictionary[nestedKey]; | ||
if (nestedValue) { | ||
[mutableQueryStringComponents addObjectsFromArray:SQQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)]; | ||
} | ||
} | ||
} else if ([value isKindOfClass:[NSArray class]]) { | ||
NSArray *array = value; | ||
for (id nestedValue in array) { | ||
[mutableQueryStringComponents addObjectsFromArray:SQQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)]; | ||
} | ||
} else if ([value isKindOfClass:[NSSet class]]) { | ||
NSSet *set = value; | ||
for (id obj in [set sortedArrayUsingDescriptors:@[ sortDescriptor ]]) { | ||
[mutableQueryStringComponents addObjectsFromArray:SQQueryStringPairsFromKeyAndValue(key, obj)]; | ||
} | ||
} else { | ||
[mutableQueryStringComponents addObject:[[SQQueryStringPair alloc] initWithField:key value:value]]; | ||
} | ||
return mutableQueryStringComponents; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.