Skip to content

Commit d46c5cd

Browse files
committed
add http
1 parent 4d86030 commit d46c5cd

File tree

14 files changed

+764
-190
lines changed

14 files changed

+764
-190
lines changed

NetDiag.xcodeproj/project.pbxproj

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
DF8DFFC51C60951D00174DA9 /* QNNNslookup.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFC41C60951D00174DA9 /* QNNNslookup.m */; };
1414
DF8DFFD21C61DA1B00174DA9 /* TcpPingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFD11C61DA1B00174DA9 /* TcpPingTest.m */; };
1515
DF8DFFD41C61DA1B00174DA9 /* libNetDiag.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DFCEC6451C2EC01D0058EE65 /* libNetDiag.a */; };
16-
DF8DFFDC1C61E6B200174DA9 /* QNTestLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFDB1C61E6B200174DA9 /* QNTestLogger.m */; };
16+
DF8DFFDC1C61E6B200174DA9 /* QNNTestLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = DF8DFFDB1C61E6B200174DA9 /* QNNTestLogger.m */; };
1717
DF9D41FA1C57787500486F87 /* QNNTcpPing.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D41F91C57787500486F87 /* QNNTcpPing.m */; };
1818
DF9D41FD1C57789B00486F87 /* QNNUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D41FC1C57789B00486F87 /* QNNUtil.m */; };
1919
DF9D42001C5778B600486F87 /* QNNRtmp.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D41FF1C5778B600486F87 /* QNNRtmp.m */; };
2020
DF9D42031C5778EA00486F87 /* QNNTraceRoute.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D42021C5778EA00486F87 /* QNNTraceRoute.m */; };
2121
DF9D42091C57BEFD00486F87 /* QNNExternalIp.m in Sources */ = {isa = PBXBuildFile; fileRef = DF9D42081C57BEFD00486F87 /* QNNExternalIp.m */; };
22+
DFA9E1761C6B350800C17111 /* QNNHttp.m in Sources */ = {isa = PBXBuildFile; fileRef = DFA9E1751C6B350800C17111 /* QNNHttp.m */; };
23+
DFA9E1781C6C8FE800C17111 /* RtmpTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFA9E1771C6C8FE800C17111 /* RtmpTest.m */; };
24+
DFA9E17A1C6DB06D00C17111 /* NslookupTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFA9E1791C6DB06D00C17111 /* NslookupTest.m */; };
25+
DFA9E17C1C6DB22C00C17111 /* libresolv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = DFA9E17B1C6DB22C00C17111 /* libresolv.tbd */; };
26+
DFA9E17E1C708A0000C17111 /* QNNHttpTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFA9E17D1C708A0000C17111 /* QNNHttpTest.m */; };
2227
DFCEC6491C2EC01D0058EE65 /* QNNetDiag.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DFCEC6481C2EC01D0058EE65 /* QNNetDiag.h */; };
2328
/* End PBXBuildFile section */
2429

@@ -57,8 +62,8 @@
5762
DF8DFFCF1C61DA1B00174DA9 /* NetDiagTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NetDiagTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5863
DF8DFFD11C61DA1B00174DA9 /* TcpPingTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TcpPingTest.m; sourceTree = "<group>"; };
5964
DF8DFFD31C61DA1B00174DA9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
60-
DF8DFFDA1C61E6B200174DA9 /* QNTestLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNTestLogger.h; sourceTree = "<group>"; };
61-
DF8DFFDB1C61E6B200174DA9 /* QNTestLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNTestLogger.m; sourceTree = "<group>"; };
65+
DF8DFFDA1C61E6B200174DA9 /* QNNTestLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNTestLogger.h; sourceTree = "<group>"; };
66+
DF8DFFDB1C61E6B200174DA9 /* QNNTestLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNTestLogger.m; sourceTree = "<group>"; };
6267
DF9D41F81C57787500486F87 /* QNNTcpPing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNTcpPing.h; sourceTree = "<group>"; };
6368
DF9D41F91C57787500486F87 /* QNNTcpPing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNTcpPing.m; sourceTree = "<group>"; };
6469
DF9D41FB1C57789B00486F87 /* QNNUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNUtil.h; sourceTree = "<group>"; };
@@ -69,6 +74,12 @@
6974
DF9D42021C5778EA00486F87 /* QNNTraceRoute.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNTraceRoute.m; sourceTree = "<group>"; };
7075
DF9D42071C57BEFD00486F87 /* QNNExternalIp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNExternalIp.h; sourceTree = "<group>"; };
7176
DF9D42081C57BEFD00486F87 /* QNNExternalIp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNExternalIp.m; sourceTree = "<group>"; };
77+
DFA9E1741C6B350800C17111 /* QNNHttp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNNHttp.h; sourceTree = "<group>"; };
78+
DFA9E1751C6B350800C17111 /* QNNHttp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNHttp.m; sourceTree = "<group>"; };
79+
DFA9E1771C6C8FE800C17111 /* RtmpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RtmpTest.m; sourceTree = "<group>"; };
80+
DFA9E1791C6DB06D00C17111 /* NslookupTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NslookupTest.m; sourceTree = "<group>"; };
81+
DFA9E17B1C6DB22C00C17111 /* libresolv.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libresolv.tbd; path = usr/lib/libresolv.tbd; sourceTree = SDKROOT; };
82+
DFA9E17D1C708A0000C17111 /* QNNHttpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNNHttpTest.m; sourceTree = "<group>"; };
7283
DFCEC6451C2EC01D0058EE65 /* libNetDiag.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libNetDiag.a; sourceTree = BUILT_PRODUCTS_DIR; };
7384
DFCEC6481C2EC01D0058EE65 /* QNNetDiag.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QNNetDiag.h; sourceTree = "<group>"; };
7485
E7FCF14D691EF09CEBD2808D /* Pods-NetDiagTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NetDiagTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-NetDiagTests/Pods-NetDiagTests.release.xcconfig"; sourceTree = "<group>"; };
@@ -80,6 +91,7 @@
8091
isa = PBXFrameworksBuildPhase;
8192
buildActionMask = 2147483647;
8293
files = (
94+
DFA9E17C1C6DB22C00C17111 /* libresolv.tbd in Frameworks */,
8395
DF8DFFD41C61DA1B00174DA9 /* libNetDiag.a in Frameworks */,
8496
A6296E24683C84AC5548DB39 /* libPods-NetDiagTests.a in Frameworks */,
8597
);
@@ -107,6 +119,7 @@
107119
7EC51E005E7B857726052BEF /* Frameworks */ = {
108120
isa = PBXGroup;
109121
children = (
122+
DFA9E17B1C6DB22C00C17111 /* libresolv.tbd */,
110123
F50835689CBA65323B9B2AA7 /* libPods-NetDiagTests.a */,
111124
);
112125
name = Frameworks;
@@ -117,8 +130,11 @@
117130
children = (
118131
DF8DFFD11C61DA1B00174DA9 /* TcpPingTest.m */,
119132
DF8DFFD31C61DA1B00174DA9 /* Info.plist */,
120-
DF8DFFDA1C61E6B200174DA9 /* QNTestLogger.h */,
121-
DF8DFFDB1C61E6B200174DA9 /* QNTestLogger.m */,
133+
DF8DFFDA1C61E6B200174DA9 /* QNNTestLogger.h */,
134+
DF8DFFDB1C61E6B200174DA9 /* QNNTestLogger.m */,
135+
DFA9E1771C6C8FE800C17111 /* RtmpTest.m */,
136+
DFA9E1791C6DB06D00C17111 /* NslookupTest.m */,
137+
DFA9E17D1C708A0000C17111 /* QNNHttpTest.m */,
122138
);
123139
path = NetDiagTests;
124140
sourceTree = "<group>";
@@ -164,6 +180,8 @@
164180
DF9D42081C57BEFD00486F87 /* QNNExternalIp.m */,
165181
DF8DFFC31C60951D00174DA9 /* QNNNslookup.h */,
166182
DF8DFFC41C60951D00174DA9 /* QNNNslookup.m */,
183+
DFA9E1741C6B350800C17111 /* QNNHttp.h */,
184+
DFA9E1751C6B350800C17111 /* QNNHttp.m */,
167185
);
168186
path = NetDiag;
169187
sourceTree = "<group>";
@@ -307,7 +325,10 @@
307325
isa = PBXSourcesBuildPhase;
308326
buildActionMask = 2147483647;
309327
files = (
310-
DF8DFFDC1C61E6B200174DA9 /* QNTestLogger.m in Sources */,
328+
DFA9E17A1C6DB06D00C17111 /* NslookupTest.m in Sources */,
329+
DF8DFFDC1C61E6B200174DA9 /* QNNTestLogger.m in Sources */,
330+
DFA9E17E1C708A0000C17111 /* QNNHttpTest.m in Sources */,
331+
DFA9E1781C6C8FE800C17111 /* RtmpTest.m in Sources */,
311332
DF8DFFD21C61DA1B00174DA9 /* TcpPingTest.m in Sources */,
312333
);
313334
runOnlyForDeploymentPostprocessing = 0;
@@ -324,6 +345,7 @@
324345
DF9D42001C5778B600486F87 /* QNNRtmp.m in Sources */,
325346
DF9D41FD1C57789B00486F87 /* QNNUtil.m in Sources */,
326347
DF9D42031C5778EA00486F87 /* QNNTraceRoute.m in Sources */,
348+
DFA9E1761C6B350800C17111 /* QNNHttp.m in Sources */,
327349
);
328350
runOnlyForDeploymentPostprocessing = 0;
329351
};

NetDiag/QNNHttp.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// QNNHttp.h
3+
// NetDiag
4+
//
5+
// Created by bailong on 16/2/10.
6+
// Copyright © 2016年 Qiniu Cloud Storage. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "QNNProtocols.h"
11+
12+
@interface QNNHttpResult : NSObject
13+
14+
@property (readonly) NSInteger code;
15+
@property (readonly) NSTimeInterval duration;
16+
@property (readonly) NSDictionary* headers;
17+
@property (readonly) NSData* body;
18+
19+
-(NSString*) description;
20+
21+
@end
22+
23+
typedef void (^QNNHttpCompleteHandler)(QNNHttpResult*);
24+
25+
@interface QNNHttp : NSObject<QNNStopDelegate>
26+
27+
/**
28+
* default port is 80
29+
*
30+
* @param host domain or ip
31+
* @param output output logger
32+
* @param complete complete callback, maybe null
33+
*
34+
* @return QNNTcpping instance, could be stop
35+
*/
36+
+(instancetype) start:(NSString*)url
37+
output:(id<QNNOutputDelegate>)output
38+
complete:(QNNHttpCompleteHandler)complete;
39+
40+
@end

NetDiag/QNNHttp.m

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//
2+
// QNNHttp.m
3+
// NetDiag
4+
//
5+
// Created by bailong on 16/2/10.
6+
// Copyright © 2016年 Qiniu Cloud Storage. All rights reserved.
7+
//
8+
9+
#import "QNNHttp.h"
10+
11+
@implementation QNNHttpResult
12+
13+
-(NSString*) description{
14+
NSString* bodySummary = @"";
15+
if (_body != nil) {
16+
NSString *str = [[NSString alloc] initWithData:_body encoding:NSUTF8StringEncoding];
17+
if (str == nil) {
18+
bodySummary = @"not utf8 string body";
19+
}
20+
}
21+
return [NSString stringWithFormat:@"code:%d duration:%f body:%@", _code, _duration, bodySummary];
22+
}
23+
24+
-(instancetype)init:(NSInteger)code
25+
duration:(NSTimeInterval)duration
26+
headers:(NSDictionary*)headers
27+
body:(NSData*)body{
28+
if(self = [super init]){
29+
_code = code;
30+
_duration = duration;
31+
_headers = headers;
32+
_body = body;
33+
}
34+
return self;
35+
}
36+
37+
38+
@end
39+
40+
@interface QNNHttp ()
41+
@property (readonly) NSString* url;
42+
@property (readonly) id<QNNOutputDelegate> output;
43+
@property (readonly) QNNHttpCompleteHandler complete;
44+
@end
45+
46+
@implementation QNNHttp
47+
48+
-(instancetype)init:(NSString*)url
49+
output:(id<QNNOutputDelegate>) output
50+
complete:(QNNHttpCompleteHandler)complete{
51+
if (self = [super init]) {
52+
_url = url;
53+
_output = output;
54+
_complete = complete;
55+
}
56+
return self;
57+
}
58+
59+
-(void)run{
60+
if (_output) {
61+
[_output write:[NSString stringWithFormat:@"GET %@", _url]];
62+
}
63+
NSDate* t1 = [NSDate date];
64+
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_url]];
65+
[urlRequest setHTTPMethod:@"GET"];
66+
67+
NSHTTPURLResponse *response = nil;
68+
NSError *httpError = nil;
69+
NSData *d = [NSURLConnection sendSynchronousRequest:urlRequest
70+
returningResponse:&response
71+
error:&httpError];
72+
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:t1]*1000;
73+
if (_output) {
74+
if (httpError != nil) {
75+
[_output write:[httpError description]];
76+
77+
}
78+
[_output write:[NSString stringWithFormat:@"complete duration:%f status %d\n", duration, response.statusCode]];
79+
if (response != nil&& response.allHeaderFields != nil) {
80+
[response.allHeaderFields enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSString *obj, BOOL *stop) {
81+
[_output write:[NSString stringWithFormat:@"%@: %@\n", key, obj]];
82+
}];
83+
}
84+
}
85+
if (_complete == nil) {
86+
return;
87+
}
88+
if (httpError != nil) {
89+
QNNHttpResult* result = [[QNNHttpResult alloc]init:httpError.code duration:duration headers:nil body:nil];
90+
_complete(result);
91+
return;
92+
}
93+
QNNHttpResult* result = [[QNNHttpResult alloc]init:response.statusCode duration:duration headers:response.allHeaderFields body:d];
94+
_complete(result);
95+
}
96+
97+
+(instancetype) start:(NSString*)url
98+
output:(id<QNNOutputDelegate>)output
99+
complete:(QNNHttpCompleteHandler)complete{
100+
QNNHttp* http = [[QNNHttp alloc] init:url output:output complete:complete];
101+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
102+
[http run];
103+
});
104+
return http;
105+
}
106+
107+
-(void)stop{
108+
109+
}
110+
111+
112+
113+
@end

NetDiag/QNNNslookup.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,44 @@
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import "QNNProtocols.h"
1011

11-
@interface QNNNslookup : NSObject
12+
/**
13+
* A 记录
14+
*/
15+
extern const int kQNNTypeA;
16+
17+
/**
18+
* Cname 记录
19+
*/
20+
extern const int kQNNTypeCname;
21+
22+
23+
@interface QNNRecord : NSObject
24+
@property (nonatomic, readonly) NSString *value;
25+
@property (readonly) int ttl;
26+
@property (readonly) int type;
27+
28+
- (instancetype)init:(NSString *)value
29+
ttl:(int)ttl
30+
type:(int)type;
31+
32+
- (NSString*) description;
1233

1334
@end
35+
36+
37+
typedef void (^QNNNslookupCompleteHandler)(NSArray*);
38+
39+
@interface QNNNslookup : NSObject<QNNStopDelegate>
40+
41+
+(instancetype) start:(NSString*)domain
42+
output:(id<QNNOutputDelegate>)output
43+
complete:(QNNNslookupCompleteHandler)complete;
44+
45+
+(instancetype) start:(NSString*)domain
46+
server:(NSString*)dnsServer
47+
output:(id<QNNOutputDelegate>)output
48+
complete:(QNNNslookupCompleteHandler)complete;
49+
50+
@end

0 commit comments

Comments
 (0)