Changeset ce14cfda814381decf6cb454e9b996745d37ffe8
- Timestamp:
- 06/07/12 15:13:38 (1 year ago)
- git-parent:
[76bc8e8991f5ed9e8336b0130c92de4d48def93c], [f291bb5ab0ef376b5f15b11df991efb2cdaeabc1]
- Files:
-
- src/modules-lua/noit/HttpClient.lua (modified) (3 diffs)
- src/modules-lua/noit/module/http.lua (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/modules-lua/noit/HttpClient.lua
r43e571f rf291bb5 86 86 local lasthdr 87 87 local str = self.e:read("\n"); 88 local cookie_count = 1; 88 89 if str == nil then error("no response") end 89 90 self.protocol, self.code = string.match(str, "^HTTP/(%d.%d)%s+(%d+)%s+") … … 91 92 self.code = tonumber(self.code) 92 93 self.headers = {} 94 self.cookies = {} 93 95 while true do 94 96 local str = self.e:read("\n") … … 104 106 else 105 107 hdr = string.lower(hdr) 106 self.headers[hdr] = val 108 if hdr == "set-cookie" then 109 self.cookies[cookie_count] = val; 110 cookie_count = cookie_count + 1; 111 else 112 self.headers[hdr] = val 113 end 107 114 lasthdr = hdr 108 115 end 109 116 end 110 if self.hooks.headers ~= nil then self.hooks.headers(self.headers ) end117 if self.hooks.headers ~= nil then self.hooks.headers(self.headers, self.cookies) end 111 118 end 112 119 src/modules-lua/noit/module/http.lua
r9889684 rf291bb5 155 155 156 156 function populate_cookie_jar(cookies, host, hdr) 157 local path = nil 157 158 if hdr ~= nil then 158 159 local name, value, trailer = … … 160 161 if name ~= nil then 161 162 local jar = { } 162 jar.name = name; 163 jar.value = value; 164 for k, v in string.gmatch(trailer, "%s*(%w+)(=%w+)?;?") do 165 if v == nil then jar[string.lower(k)] = true 166 else jar[string.lower(k)] = v:sub(2) 163 local fields = noit.extras.split(trailer, ";") 164 if fields ~= nil then 165 for k, v in pairs(fields) do 166 local pair = noit.extras.split(v, "=", 1); 167 if pair ~= nil and pair[1] ~= nil and pair[2] ~= nil then 168 local name = (string.gsub(pair[1], "^%s*(.-)%s*$", "%1")); 169 local setting = (string.gsub(pair[2], "^%s*(.-)%s*$", "%1")); 170 if name == "path" then 171 path = setting 172 end 173 end 167 174 end 168 175 end 169 if jar.domain ~= nil then host = jar.domain end 170 if cookies[host] == nil then cookies[host] = { } end 171 table.insert(cookies[host], jar) 176 if string.sub(name, 1, 1) ~= ";" and string.sub(value, 1, 1) ~= ";" then 177 if path == nil then path = "/" end 178 if cookies[host] == nil then cookies[host] = { } end 179 if cookies[host][path] == nil then cookies[host][path] = { } end 180 jar.name = name 181 jar.value = value 182 table.insert(cookies[host][path], jar) 183 end 172 184 end 173 185 end … … 187 199 188 200 function apply_cookies(headers, cookies, host, uri) 189 for h, jars in pairs(cookies) do 201 local use_cookies = { } 202 for h, paths in pairs(cookies) do 190 203 if has_host(h, host) then 191 for i, jar in ipairs(jars) do 192 if jar.path == nil or 193 uri:sub(1, jar.path:len()) == jar.path then 194 if headers["Cookie"] == nil then 195 headers["Cookie"] = jar.name .. "=" .. jar.value 196 else 197 headers["Cookie"] = headers["Cookie"] .. "; " .. 198 jar.name .. "=" .. jar.value 204 local split_uri = noit.extras.split(uri, "/") 205 if split_uri ~= nil then 206 local path = "" 207 for i, val in pairs(split_uri) do 208 local append = true 209 if val == nil then val = "" end 210 if #split_uri == i and string.find(val, "%.") ~= nil then append = false end 211 if append == true then 212 path = path .. "/" .. val 213 if string.len(path) >= 2 and string.sub(path, 1, 2) == "//" then 214 path = string.sub(path, 2) 215 end 216 end 217 if path == "" then path = "/" end 218 local rindex = string.match(path, '.*()'..'%?') 219 if rindex ~= nil then 220 path = string.sub(path, 1, rindex-1) 221 end 222 if path ~= "/" then 223 while string.find(path, "/", -1) ~= nil do 224 path = string.sub(path, 1, -2) 225 end 226 end 227 if paths[path] ~= nil then 228 local jars = paths[path] 229 for index, jar in ipairs(jars) do 230 use_cookies[jar.name] = jar.value 231 end 199 232 end 200 233 end … … 202 235 end 203 236 end 237 for name, value in pairs(use_cookies) do 238 if headers["Cookie"] == nil then 239 headers["Cookie"] = name .. "=" .. value 240 else 241 headers["Cookie"] = headers["Cookie"] .. "; " .. name .. "=" .. value 242 end 243 end 244 end 245 246 function get_new_uri(old_uri, new_uri) 247 if new_uri == nil then return "/" end 248 if new_uri == "/" then return new_uri end 249 local toReturn = old_uri 250 while string.find(toReturn, "/", -1) ~= nil do 251 toReturn = string.sub(toReturn, 1, -2) 252 end 253 if string.sub(new_uri, 1, 1) == '?' then 254 local rindex = string.match(toReturn, '.*()'.."/") 255 toReturn = string.sub(toReturn, 1, rindex-1) 256 toReturn = toReturn .. new_uri 257 elseif string.sub(new_uri, 1, 1) ~= "." then 258 toReturn = new_uri 259 else 260 toReturn = string.gsub(toReturn, "%/%?", "?") 261 while string.sub(new_uri, 1, 1) == "." do 262 if string.find(new_uri, "%./") == 1 then 263 new_uri = string.gsub("%./", "", 1) 264 elseif string.find(new_uri, "%.%./") == 1 then 265 --strip out last bit from toReturn 266 local rindex = string.match(toReturn, '.*()'.."/") 267 toReturn = string.sub(toReturn, 1, rindex-1) 268 new_uri = string.gsub(new_uri, "../", "", 1) 269 else 270 -- bad URI... just return / 271 return "/" 272 end 273 end 274 toReturn = toReturn .. "/" .. new_uri 275 end 276 return toReturn 277 end 278 279 function get_absolute_path(uri) 280 if uri == nil then return "/" end 281 local toReturn = uri 282 local go_back = string.find(toReturn, "%.%./") 283 while go_back ~= nil do 284 local tojoin = go_back + 3 285 go_back = go_back - 2 286 local back_substring = string.sub(toReturn, 1, go_back) 287 local forward_substring = string.sub(toReturn, tojoin) 288 local rindex = string.match(back_substring, '.*()' .. "/") 289 if rindex ~= nil then 290 toReturn = string.sub(toReturn, 1, rindex) .. forward_substring 291 end 292 go_back = string.find(toReturn, "%.%./") 293 end 294 toReturn = string.gsub(toReturn, "%./", "") 295 return toReturn 204 296 end 205 297 … … 263 355 output = output .. (str or '') 264 356 end 265 callbacks.headers = function (hdrs )357 callbacks.headers = function (hdrs, setcookies) 266 358 next_location = hdrs.location 267 populate_cookie_jar(cookies, host, hdrs["set-cookie"]) 268 populate_cookie_jar(cookies, hdrs["set-cookie2"]) 359 for key, value in pairs(setcookies) do 360 populate_cookie_jar(cookies, host, value) 361 end 269 362 end 270 363 … … 363 456 local prev_port = port 364 457 local prev_host = host 458 local prev_uri = uri 365 459 method = 'GET' 366 460 payload = nil … … 371 465 port = prev_port 372 466 host = prev_host 373 uri = next_location467 uri = get_new_uri(prev_uri, next_location) 374 468 elseif schema == 'http' then 375 469 use_ssl = false … … 379 473 if port == "" then port = 443 end 380 474 end 475 uri = get_absolute_path(uri) 381 476 if host ~= nil then 382 477 headers.Host = host 383 478 local r = dns:lookup(host) 384 479 if not r or r.a == nil then 385 check.status("failed to resolve " +host)480 check.status("failed to resolve " .. host) 386 481 return 387 482 end 388 483 target = r.a 484 end 485 while string.find(host, "/", -1) ~= nil do 486 host = string.sub(host, 1, -2) 389 487 end 390 488 headers["Cookie"] = check.config["header_Cookie"]
