Skip to content

Commit 75ee0b3

Browse files
committed
add url encode for video and request
1 parent fe70165 commit 75ee0b3

File tree

7 files changed

+142
-6
lines changed

7 files changed

+142
-6
lines changed

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/LuaView/VPLuaPlayer.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import <AVFoundation/AVFoundation.h>
1515

1616
NSString * const VPLuaPauseVideoPlayerNotification = @"VPLuaPauseVideoPlayerNotification";
17+
#import "VPUPUrlUtil.h"
1718

1819
@interface VPLuaPlayer () <VPUPVideoClipProtocol>
1920

@@ -114,7 +115,7 @@ static int source (lua_State *L) {
114115
NSString *url = lv_paramString(L, 2);// 2
115116
if( [player isKindOfClass:[VPLuaPlayer class]] ){
116117
VPUPVideo *video = [[VPUPVideo alloc] init];
117-
video.url = [NSURL URLWithString:url];
118+
video.url = [NSURL URLWithString:[VPUPUrlUtil urlencode:url]];
118119
player.videoArray = @[video];
119120
return 0;
120121
}
@@ -140,7 +141,7 @@ static int videoPlay (lua_State *L) {
140141
NSString *url = lv_paramString(L, 2);// 2
141142
if( [player isKindOfClass:[VPLuaPlayer class]] ){
142143
VPUPVideo *video = [[VPUPVideo alloc] init];
143-
video.url = [NSURL URLWithString:url];
144+
video.url = [NSURL URLWithString:[VPUPUrlUtil urlencode:url]];
144145
player.videoArray = @[video];
145146
}
146147
}

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaHttpRequest.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "VPLuaNetworkManager.h"
1818
#import "VPUPMD5Util.h"
1919
#import "VPUPRandomUtil.h"
20+
#import "VPUPUrlUtil.h"
2021

2122
@interface VPLuaHttpRequest()
2223

@@ -155,6 +156,8 @@ static int httpRequest(lua_State *L, VPUPRequestMethodType methodType) {
155156
return 0;
156157
}
157158

159+
url = [VPUPUrlUtil urlencode:url];
160+
158161
baseUrl = [[NSURL URLWithString:@"/" relativeToURL:[NSURL URLWithString:url]].absoluteString copy];
159162
requestMethod = [url stringByReplacingOccurrencesOfString:baseUrl withString:@""];
160163

@@ -301,6 +304,8 @@ static int upload(lua_State *L) {
301304
filepath = lv_paramString(L, 3);
302305
}
303306

307+
requestUrl = [VPUPUrlUtil urlencode:requestUrl];
308+
304309
//重组function的key,保证不重复
305310
__block NSString *bRequestMethod = [[VPUPMD5Util md5_16bitHashString:[NSString stringWithFormat:@"%@%@", requestUrl, filepath]] stringByAppendingString:[VPUPRandomUtil randomStringByLength:3]];
306311
[LVUtil registryValue:L key:bRequestMethod stack:4];

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaNativeBridge.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
#import "VPLuaMacroDefine.h"
5858
#import "VPLuaSDK.h"
59+
#import "VPUPUrlUtil.h"
5960

6061
#define IS_IOS11 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)
6162

@@ -294,6 +295,7 @@ +(int) lvClassDefine:(lua_State *)L globalName:(NSString*) globalName{
294295
{"preloadVideo", preloadVideo},
295296
{"copyStringToPasteBoard", copyStringToPasteBoard},
296297
{"videoOShost", videoOShost},
298+
{"isCacheVideo", isCacheVideo},
297299
{NULL, NULL}
298300
};
299301
lv_createClassMetaTable(L,META_TABLE_NativeObject);
@@ -652,6 +654,8 @@ static int httpRequest(lua_State *L, VPUPRequestMethodType methodType) {
652654
return 0;
653655
}
654656

657+
url = [VPUPUrlUtil urlencode:url];
658+
655659
baseUrl = [[NSURL URLWithString:@"/" relativeToURL:[NSURL URLWithString:url]].absoluteString copy];
656660
requestMethod = [url stringByReplacingOccurrencesOfString:baseUrl withString:@""];
657661

@@ -759,6 +763,8 @@ static int upload(lua_State *L) {
759763
filepath = lv_paramString(L, 3);
760764
}
761765

766+
requestUrl = [VPUPUrlUtil urlencode:requestUrl];
767+
762768
//重组function的key,保证不重复
763769
__block NSString *bRequestMethod = [[VPUPMD5Util md5_16bitHashString:[NSString stringWithFormat:@"%@%@", requestUrl, filepath]] stringByAppendingString:[VPUPRandomUtil randomStringByLength:3]];
764770
[LVUtil registryValue:L key:bRequestMethod stack:4];
@@ -1347,4 +1353,27 @@ static int videoOShost(lua_State *L) {
13471353
return 1;
13481354
}
13491355

1356+
static int isCacheVideo(lua_State *L) {
1357+
BOOL fileExists = NO;
1358+
if( lua_gettop(L) >= 2) {
1359+
if (lua_type(L, 2) == LUA_TSTRING) {
1360+
NSString *urlString = lv_paramString(L, 2);
1361+
1362+
NSURL *url = [NSURL URLWithString:[VPUPUrlUtil urlencode:urlString]];
1363+
1364+
if (url) {
1365+
NSString *fileName = [NSString stringWithFormat:@"%@.%@",[VPUPMD5Util md5HashString:url.absoluteString],[url pathExtension]];
1366+
1367+
NSString *destinationPath = [VPUPPathUtil pathByPlaceholder:@"videoAds"];
1368+
1369+
NSString *videoPath = [NSString stringWithFormat:@"%@/%@", destinationPath, fileName];
1370+
1371+
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:videoPath];
1372+
}
1373+
}
1374+
}
1375+
lua_pushboolean(L, fileExists);
1376+
return 1;
1377+
}
1378+
13501379
@end

VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,10 @@
652652
D0150B67206CD378008CD3FD /* VPUPFLAnimatedFrameImage.m in Sources */ = {isa = PBXBuildFile; fileRef = D0150B63206CD378008CD3FD /* VPUPFLAnimatedFrameImage.m */; };
653653
D0150B68206CD378008CD3FD /* VPUPFLAnimatedFrameImage.m in Sources */ = {isa = PBXBuildFile; fileRef = D0150B63206CD378008CD3FD /* VPUPFLAnimatedFrameImage.m */; };
654654
D0150B69206CD378008CD3FD /* VPUPFLAnimatedFrameImage.m in Sources */ = {isa = PBXBuildFile; fileRef = D0150B63206CD378008CD3FD /* VPUPFLAnimatedFrameImage.m */; };
655+
D01E219F223E8EF90018A1AE /* VPUPUrlUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = D01E219D223E8EF90018A1AE /* VPUPUrlUtil.h */; };
656+
D01E21A0223E8EF90018A1AE /* VPUPUrlUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = D01E219D223E8EF90018A1AE /* VPUPUrlUtil.h */; };
657+
D01E21A1223E8EF90018A1AE /* VPUPUrlUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = D01E219E223E8EF90018A1AE /* VPUPUrlUtil.m */; };
658+
D01E21A2223E8EF90018A1AE /* VPUPUrlUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = D01E219E223E8EF90018A1AE /* VPUPUrlUtil.m */; };
655659
D022A0A720BE6CA600F46029 /* VPUPFileHandle.m in Sources */ = {isa = PBXBuildFile; fileRef = D022A0A120BE6CA500F46029 /* VPUPFileHandle.m */; };
656660
D022A0A820BE6CA600F46029 /* VPUPFileHandle.m in Sources */ = {isa = PBXBuildFile; fileRef = D022A0A120BE6CA500F46029 /* VPUPFileHandle.m */; };
657661
D022A0A920BE6CA600F46029 /* VPUPFileHandle.m in Sources */ = {isa = PBXBuildFile; fileRef = D022A0A120BE6CA500F46029 /* VPUPFileHandle.m */; };
@@ -1243,6 +1247,8 @@
12431247
D0150B5A206CC312008CD3FD /* VPUPLoadImageSDManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VPUPLoadImageSDManager.m; sourceTree = "<group>"; };
12441248
D0150B62206CD378008CD3FD /* VPUPFLAnimatedFrameImage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VPUPFLAnimatedFrameImage.h; sourceTree = "<group>"; };
12451249
D0150B63206CD378008CD3FD /* VPUPFLAnimatedFrameImage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VPUPFLAnimatedFrameImage.m; sourceTree = "<group>"; };
1250+
D01E219D223E8EF90018A1AE /* VPUPUrlUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VPUPUrlUtil.h; sourceTree = "<group>"; };
1251+
D01E219E223E8EF90018A1AE /* VPUPUrlUtil.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VPUPUrlUtil.m; sourceTree = "<group>"; };
12461252
D022A0A120BE6CA500F46029 /* VPUPFileHandle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VPUPFileHandle.m; sourceTree = "<group>"; };
12471253
D022A0A220BE6CA500F46029 /* VPUPVideoRequestTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VPUPVideoRequestTask.h; sourceTree = "<group>"; };
12481254
D022A0A320BE6CA600F46029 /* VPUPAVAssetResourceLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VPUPAVAssetResourceLoader.h; sourceTree = "<group>"; };
@@ -1588,6 +1594,8 @@
15881594
D0928D08208ED28D008AB5EC /* VPUPDeviceUtil.m */,
15891595
D0BD1AB52130129F00A087F2 /* VPUPIPAddressUtil.h */,
15901596
D0BD1AB62130129F00A087F2 /* VPUPIPAddressUtil.m */,
1597+
D01E219D223E8EF90018A1AE /* VPUPUrlUtil.h */,
1598+
D01E219E223E8EF90018A1AE /* VPUPUrlUtil.m */,
15911599
);
15921600
name = Utils;
15931601
path = VideoPlsUtilsPlatformSDK/Common/Utils;
@@ -2130,6 +2138,7 @@
21302138
D004F72B2099567700022E90 /* vpup_mosquitto_internal.h in Headers */,
21312139
D02832161FDA310300A705C2 /* VPUPClickThroughView.h in Headers */,
21322140
D004F6FB2099567700022E90 /* vpup_util_mosq.h in Headers */,
2141+
D01E219F223E8EF90018A1AE /* VPUPUrlUtil.h in Headers */,
21332142
0655035C1EFA28A0006FBADB /* VPUPPrefetchManager.h in Headers */,
21342143
0655035D1EFA28A0006FBADB /* VPUPPrefetchImageManager.h in Headers */,
21352144
0655035E1EFA28A0006FBADB /* VPUPPrefetchVideoManager.h in Headers */,
@@ -2496,6 +2505,7 @@
24962505
0655035F1EFA28AD006FBADB /* VPUPPrefetchManager.h in Headers */,
24972506
065503601EFA28AD006FBADB /* VPUPPrefetchImageManager.h in Headers */,
24982507
D004F6FC2099567700022E90 /* vpup_util_mosq.h in Headers */,
2508+
D01E21A0223E8EF90018A1AE /* VPUPUrlUtil.h in Headers */,
24992509
065503611EFA28AD006FBADB /* VPUPPrefetchVideoManager.h in Headers */,
25002510
063FDB931F0E17E800333B46 /* VPUPFLAnimatedImage.h in Headers */,
25012511
D0928CC92088A0C2008AB5EC /* VPUPSDWebImageCoderHelper.h in Headers */,
@@ -2897,6 +2907,7 @@
28972907
D0EE644420411161005D53C5 /* VPUPInterfaceDataServiceManager.m in Sources */,
28982908
D022A0B020BE6CA600F46029 /* VPUPVideoRequestTask.m in Sources */,
28992909
D05369C320576B6200A4F8C6 /* VPUPHTTPHost.m in Sources */,
2910+
D01E21A1223E8EF90018A1AE /* VPUPUrlUtil.m in Sources */,
29002911
06789B171EE979530052A705 /* VPUPPrefetchVideoManager.m in Sources */,
29012912
D004F7042099567700022E90 /* vpup_read_handle_client.c in Sources */,
29022913
D0BD1AAD212FE60600A087F2 /* VPUPRSAUtil.m in Sources */,
@@ -3215,6 +3226,7 @@
32153226
064D56DB1EC839B600C9DEC4 /* VPUPCipherOperation.m in Sources */,
32163227
D004F66420931A8C00022E90 /* VPUPPersistentConnectionFactory.m in Sources */,
32173228
D022A0B120BE6CA600F46029 /* VPUPVideoRequestTask.m in Sources */,
3229+
D01E21A2223E8EF90018A1AE /* VPUPUrlUtil.m in Sources */,
32183230
06FD507F1EC1AA2800C65505 /* VPUPHTTPBaseAPI.m in Sources */,
32193231
D0EE644520411161005D53C5 /* VPUPInterfaceDataServiceManager.m in Sources */,
32203232
D0BD1AAE212FE60600A087F2 /* VPUPRSAUtil.m in Sources */,

VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/Common/PrefetchManager/VPUPPrefetchVideoManager.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "VPUPFileHandle.h"
1111
#import "VPUPMD5Util.h"
1212
#import "VPUPPathUtil.h"
13+
#import "VPUPUrlUtil.h"
1314

1415
@implementation VPUPPrefetchVideoManager
1516

@@ -25,15 +26,21 @@ - (void)prefetchURLs:(NSArray<NSString *> *)urls {
2526
if (urls && urls.count == 0) {
2627
return;
2728
}
29+
2830
NSMutableArray *fileNames = [NSMutableArray arrayWithCapacity:0];
31+
NSMutableArray *encodeUrls = [NSMutableArray arrayWithCapacity:0];
2932
for (NSString *urlString in urls) {
30-
NSURL *url = [NSURL URLWithString:urlString];
31-
NSString *fileName = [NSString stringWithFormat:@"%@.%@",[VPUPMD5Util md5HashString:url.absoluteString],[url pathExtension]];
32-
[fileNames addObject:fileName];
33+
NSString *urlEncodeString = [VPUPUrlUtil urlencode:urlString];
34+
NSURL *url = [NSURL URLWithString:urlEncodeString];
35+
if (url) {
36+
NSString *fileName = [NSString stringWithFormat:@"%@.%@",[VPUPMD5Util md5HashString:url.absoluteString],[url pathExtension]];
37+
[fileNames addObject:fileName];
38+
}
39+
[encodeUrls addObject:urlEncodeString];
3340
}
3441

3542
NSString *destinationPath = [VPUPPathUtil pathByPlaceholder:@"videoAds"];
36-
[self prefetchURLs:urls fileNames:fileNames destinationPath:destinationPath];
43+
[self prefetchURLs:encodeUrls fileNames:fileNames destinationPath:destinationPath];
3744
}
3845

3946
@end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// VPUPUrlUtil.h
3+
// VideoPlsUtilsPlatformSDK
4+
//
5+
// Created by peter on 2019/3/17.
6+
// Copyright © 2019 videopls. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface VPUPUrlUtil : NSObject
14+
15+
+ (NSString *)urlencode:(NSString *)url;
16+
17+
@end
18+
19+
NS_ASSUME_NONNULL_END
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// VPUPUrlUtil.m
3+
// VideoPlsUtilsPlatformSDK
4+
//
5+
// Created by peter on 2019/3/17.
6+
// Copyright © 2019 videopls. All rights reserved.
7+
//
8+
9+
#import "VPUPUrlUtil.h"
10+
11+
@implementation VPUPUrlUtil
12+
13+
NSString * VPUPPercentEscapedStringFromString(NSString *string) {
14+
static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
15+
static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";
16+
17+
NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
18+
[allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]];
19+
20+
// FIXME: https://github.com/AFNetworking/AFNetworking/pull/3028
21+
// return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
22+
23+
static NSUInteger const batchSize = 50;
24+
25+
NSUInteger index = 0;
26+
NSMutableString *escaped = @"".mutableCopy;
27+
28+
while (index < string.length) {
29+
NSUInteger length = MIN(string.length - index, batchSize);
30+
NSRange range = NSMakeRange(index, length);
31+
32+
// To avoid breaking up character sequences such as 👴🏻👮🏽
33+
range = [string rangeOfComposedCharacterSequencesForRange:range];
34+
35+
NSString *substring = [string substringWithRange:range];
36+
NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
37+
[escaped appendString:encoded];
38+
39+
index += range.length;
40+
}
41+
return escaped;
42+
}
43+
44+
+ (NSString *)urlencode:(NSString *)url {
45+
if (!url) {
46+
return nil;
47+
}
48+
NSString *urlString = nil;
49+
if ([url rangeOfString:@"https://"].location == 0) {
50+
urlString = [NSString stringWithFormat:@"https://%@",VPUPPercentEscapedStringFromString([url substringFromIndex:8])];
51+
}
52+
else if ([url rangeOfString:@"http://"].location == 0) {
53+
urlString = [NSString stringWithFormat:@"http://%@",VPUPPercentEscapedStringFromString([url substringFromIndex:7])];
54+
}
55+
56+
if (!urlString) {
57+
urlString = VPUPPercentEscapedStringFromString(url);
58+
}
59+
60+
return urlString;
61+
}
62+
63+
@end

0 commit comments

Comments
 (0)