File tree 3 files changed +34
-16
lines changed
3 files changed +34
-16
lines changed Original file line number Diff line number Diff line change @@ -54,20 +54,6 @@ function M.load_jar()
54
54
end
55
55
end
56
56
57
- --- parse url to domain and path
58
- --- path will be fallback to "/" if not found
59
- --- @param url string
60
- --- @return string domain
61
- --- @return string path
62
- local function parse_url (url )
63
- local domain , path = url :match (" ^%a+://([^/]+)(/[^?#]*)$" )
64
- if not path then
65
- domain = url :match (" ^%a+://([^/]+)" )
66
- path = " /"
67
- end
68
- return domain , path
69
- end
70
-
71
57
--- @private
72
58
--- parse Set-Cookie header to cookie
73
59
--- @param req_url string request URL to be used as fallback domain & path of cookie
@@ -105,7 +91,7 @@ function M.parse_set_cookie(req_url, header)
105
91
cookie .priority = val
106
92
end
107
93
end
108
- cookie .domain = cookie .domain or (" ." .. req_url : match ( " ^%a+://([^/]+) " ))
94
+ cookie .domain = cookie .domain or (" ." .. utils . parse_url ( req_url ))
109
95
cookie .path = cookie .path or " /"
110
96
if max_age == - 1 then
111
97
cookie .expires = - 1
@@ -184,7 +170,7 @@ function M.save_jar()
184
170
end
185
171
186
172
local function match_cookie (url , cookie )
187
- local req_domain , req_path = parse_url (url )
173
+ local req_domain , req_path = utils . parse_url (url )
188
174
if not req_domain then
189
175
return false
190
176
end
Original file line number Diff line number Diff line change @@ -105,6 +105,17 @@ function utils.parse_http_time(time_str)
105
105
return os.time (time_table ) + gmt_offset
106
106
end
107
107
108
+ --- parse url to domain and path
109
+ --- path will be fallback to "/" if not found
110
+ --- @param url string
111
+ --- @return string domain
112
+ --- @return string path
113
+ function utils .parse_url (url )
114
+ local domain = url :match (" ^%a+://([^/]+)" ) or url :match (" ^([^/]+)" )
115
+ local path = url :match (" [^:/]+(/[^?#]*)" ) or " /"
116
+ return domain , path
117
+ end
118
+
108
119
--- Default transformers for statistics
109
120
local transform = {
110
121
--- Transform `time` into a readable typed time (e.g. 200ms)
Original file line number Diff line number Diff line change @@ -7,6 +7,27 @@ local function open(path)
7
7
return 0
8
8
end
9
9
10
+ describe (" basic utils" , function ()
11
+ it (" parse_url" , function ()
12
+ local function parse_url (url )
13
+ local domain , path = utils .parse_url (url )
14
+ return { domain = domain , path = path }
15
+ end
16
+ assert .same ({
17
+ domain = " domain.com" ,
18
+ path = " /some/path" ,
19
+ }, parse_url (" http://domain.com/some/path?query=value" ))
20
+ assert .same ({
21
+ domain = " domain.com" ,
22
+ path = " /some/path" ,
23
+ }, parse_url (" domain.com/some/path?query=value" ))
24
+ assert .same ({
25
+ domain = " localhost:8000" ,
26
+ path = " /some/path" ,
27
+ }, parse_url (" localhost:8000/some/path?query=value" ))
28
+ end )
29
+ end )
30
+
10
31
describe (" tree-sitter utils" , function ()
11
32
local source = open (" spec/examples/script/post_request_script.http" )
12
33
it (" ts_parse_source" , function ()
You can’t perform that action at this time.
0 commit comments