Skip to content

HTTP add headers #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 9 additions & 34 deletions NetDiag.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@
DF8DFFCB1C61DA1B00174DA9 /* Sources */,
DF8DFFCC1C61DA1B00174DA9 /* Frameworks */,
DF8DFFCD1C61DA1B00174DA9 /* Resources */,
6360AC3809EC45C17B6B9A39 /* [CP] Embed Pods Frameworks */,
7EB1F7F29D34632701B40C83 /* [CP] Copy Pods Resources */,
D1E68BCE0991EB5A44C81ED8 /* Embed Pods Frameworks */,
3317CC6757C386790A56DF6E /* Copy Pods Resources */,
);
Expand Down Expand Up @@ -243,7 +241,7 @@
DFCEC63D1C2EC01D0058EE65 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0920;
LastUpgradeCheck = 1020;
ORGANIZATIONNAME = "Qiniu Cloud Storage";
TargetAttributes = {
DF8DFFCE1C61DA1B00174DA9 = {
Expand All @@ -257,10 +255,11 @@
};
buildConfigurationList = DFCEC6401C2EC01D0058EE65 /* Build configuration list for PBXProject "NetDiag" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = DFCEC63C1C2EC01D0058EE65;
productRefGroup = DFCEC6461C2EC01D0058EE65 /* Products */;
Expand Down Expand Up @@ -317,36 +316,6 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
6360AC3809EC45C17B6B9A39 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
7EB1F7F29D34632701B40C83 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
9F9B0470989AE72C26C6B154 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -458,6 +427,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand All @@ -466,12 +436,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -509,6 +481,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand All @@ -517,12 +490,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
4 changes: 1 addition & 3 deletions NetDiag.xcodeproj/xcshareddata/xcschemes/NetDiag.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0920"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -56,7 +55,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0920"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand All @@ -47,7 +46,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
8 changes: 8 additions & 0 deletions NetDiag.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
3 changes: 2 additions & 1 deletion NetDiag/QNNHttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ typedef void (^QNNHttpCompleteHandler)(QNNHttpResult*);
* @return QNNTcpping instance, could be stop
*/
+ (instancetype)start:(NSString*)url
headers:(NSDictionary *)headers
output:(id<QNNOutputDelegate>)output
complete:(QNNHttpCompleteHandler)complete;

@end
@end
11 changes: 9 additions & 2 deletions NetDiag/QNNHttp.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,22 @@ - (instancetype)init:(NSInteger)code

@interface QNNHttp ()
@property (readonly) NSString *url;
@property (readonly) NSDictionary *headers;
@property (readonly) id<QNNOutputDelegate> output;
@property (readonly) QNNHttpCompleteHandler complete;
@end

@implementation QNNHttp

- (instancetype)init:(NSString *)url
headers:(NSDictionary *)headers
output:(id<QNNOutputDelegate>)output
complete:(QNNHttpCompleteHandler)complete {
if (self = [super init]) {
_url = url == nil ? @"" : url;
_output = output;
_complete = complete;
_headers = headers;
}
return self;
}
Expand Down Expand Up @@ -96,6 +99,9 @@ - (void)run {
NSDate *t1 = [NSDate date];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_url]];
[urlRequest setHTTPMethod:@"GET"];
[_headers enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
[urlRequest setValue:value forHTTPHeaderField:key];
}];

NSHTTPURLResponse *response = nil;
NSError *httpError = nil;
Expand All @@ -110,7 +116,7 @@ - (void)run {
[_output write:[NSString stringWithFormat:@"complete duration:%f status %ld\n", duration, (long)response.statusCode]];
if (response != nil && response.allHeaderFields != nil) {
[response.allHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[_output write:[NSString stringWithFormat:@"%@: %@\n", key, obj]];
[self->_output write:[NSString stringWithFormat:@"%@: %@\n", key, obj]];
}];
}
}
Expand All @@ -128,12 +134,13 @@ - (void)run {
}

+ (instancetype)start:(NSString *)url
headers:(NSDictionary *) headers
output:(id<QNNOutputDelegate>)output
complete:(QNNHttpCompleteHandler)complete {
if (url == nil) {
url = @"";
}
QNNHttp *http = [[QNNHttp alloc] init:url output:output complete:complete];
QNNHttp *http = [[QNNHttp alloc] init:url headers:headers output:output complete:complete];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
[http run];
});
Expand Down
6 changes: 3 additions & 3 deletions NetDiag/QNNPing.m
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ - (void)run {
[self.output write:@"Problem accessing the DNS"];
if (_complete != nil) {
[QNNQue async_run_main:^(void) {
QNNPingResult *result = [[QNNPingResult alloc] init:-1006 ip:nil size:_size max:0 min:0 avg:0 loss:0 count:0 totalTime:0 stddev:0];
_complete(result);
QNNPingResult *result = [[QNNPingResult alloc] init:-1006 ip:nil size:self->_size max:0 min:0 avg:0 loss:0 count:0 totalTime:0 stddev:0];
self->_complete(result);
}];
}
return;
Expand Down Expand Up @@ -385,7 +385,7 @@ - (void)run {
totalTime:[[NSDate date] timeIntervalSinceDate:begin] * 1000];
[self.output write:result.description];
[QNNQue async_run_main:^(void) {
_complete(result);
self->_complete(result);
}];
}
free(durations);
Expand Down
4 changes: 2 additions & 2 deletions NetDiag/QNNRtmp.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ - (void)run {
[self.output write:@"Problem accessing the DNS"];
if (_complete != nil) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
_complete([self buildResult:-1006 durations:nil count:0]);
self->_complete([self buildResult:-1006 durations:nil count:0]);
});
}
return;
Expand Down Expand Up @@ -264,7 +264,7 @@ - (void)run {
code = kQNNRequestStoped;
}
dispatch_async(dispatch_get_main_queue(), ^(void) {
_complete([self buildResult:code durations:intervals count:index]);
self->_complete([self buildResult:code durations:intervals count:index]);
});
}
free(intervals);
Expand Down
4 changes: 2 additions & 2 deletions NetDiag/QNNTcpPing.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ - (void)run {
[self.output write:@"Problem accessing the DNS"];
if (_complete != nil) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
_complete([self buildResult:-1006 ip:nil durations:nil loss:0 count:0 totalTime:0]);
self->_complete([self buildResult:-1006 ip:nil durations:nil loss:0 count:0 totalTime:0]);
});
}
return;
Expand Down Expand Up @@ -149,7 +149,7 @@ - (void)run {
}
__block NSDate *startDate = begin;
dispatch_async(dispatch_get_main_queue(), ^(void) {
_complete([self buildResult:code ip:ip durations:intervals loss:loss count:index totalTime:[[NSDate date] timeIntervalSinceDate:startDate] * 1000]);
self->_complete([self buildResult:code ip:ip durations:intervals loss:loss count:index totalTime:[[NSDate date] timeIntervalSinceDate:startDate] * 1000]);
free(intervals);
});
}
Expand Down
8 changes: 4 additions & 4 deletions NetDiag/QNNTraceRoute.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ - (void)run {
if (_complete != nil) {
[QNNQue async_run_main:^(void) {
QNNTraceRouteResult* result = [[QNNTraceRouteResult alloc] init:-1006 ip:nil content:nil];
_complete(result);
self->_complete(result);
}];
}
return;
Expand All @@ -199,7 +199,7 @@ - (void)run {
if (_complete != nil) {
[QNNQue async_run_main:^(void) {
QNNTraceRouteResult* result = [[QNNTraceRouteResult alloc] init:-1 ip:[NSString stringWithUTF8String:inet_ntoa(addr.sin_addr)] content:nil];
_complete(result);
self->_complete(result);
}];
}
close(recv_sock);
Expand Down Expand Up @@ -227,8 +227,8 @@ - (void)run {
code = kQNNRequestStoped;
}
[QNNQue async_run_main:^(void) {
QNNTraceRouteResult* result = [[QNNTraceRouteResult alloc] init:code ip:[NSString stringWithUTF8String:inet_ntoa(addr.sin_addr)] content:_contentString];
_complete(result);
QNNTraceRouteResult* result = [[QNNTraceRouteResult alloc] init:code ip:[NSString stringWithUTF8String:inet_ntoa(addr.sin_addr)] content:self->_contentString];
self->_complete(result);
}];
}

Expand Down
3 changes: 2 additions & 1 deletion NetDiagTests/QNNHttpTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ - (void)tearDown {

- (void)testOK {
__block BOOL run = NO;
[QNNHttp start:@"http://www.baidu.com" output:[[QNNTestLogger alloc] init] complete:^(QNNHttpResult* r) {

[QNNHttp start:@"http://www.baidu.com" headers:nil output:[[QNNTestLogger alloc] init] complete:^(QNNHttpResult* r) {
XCTAssertNotNil(r, @"need result");
XCTAssertNotNil(r.ip, @"need ip");
XCTAssertNotNil(r.headers, @"need headers");
Expand Down