http: Windows fixes

pull/1103/head
Alexander Medvednikov 2019-07-12 16:26:02 +02:00
parent 7d5f29dbd1
commit 5baff9beda
1 changed files with 18 additions and 17 deletions

View File

@ -4,9 +4,11 @@
module http
#flag -lwininet
import time
#flag -lwininet -lShlwapi
#flag -lurlmon
// #include <WinInet.h>
#include <WinInet.h>
#include "urlmon.h"
#include <shlwapi.h>
// #LPWSTR winstring(string s);
@ -22,10 +24,10 @@ fn (req &Request) do() Response {
mut s := ''
emptyresp := Response{}
mut url := req.url
println('\n\nhttp.do() WIN URL="$url" TYP=$req.typ data="$req.data" headers.len=req.headers.len"')
println(req.headers)
//println('\n\nhttp.do() WIN URL="$url" TYP=$req.typ data="$req.data" headers.len=req.headers.len"')
//println(req.headers)
is_ssl := req.url.starts_with('https://')
println('is ssl=$is_ssl')
//println('is ssl=$is_ssl')
mut pos := url.index('/')
url = url.right(pos + 2)
mut host := url
@ -116,7 +118,7 @@ fn (req &Request) do() Response {
// LPVOID lpOutBuffer=malloc(dwSize);
# HttpQueryInfo(request, HTTP_QUERY_RAW_HEADERS_CRLF,
# h_buf,&dwSize,NULL);
# printf(" resp HEADERS %s\n", h_buf);
//# printf(" resp HEADERS %s\n", h_buf);
// Get response body
// # const int BUF_MAX = 1024;
// # TCHAR buf[BUF_MAX + 1];
@ -137,15 +139,13 @@ fn (req &Request) do() Response {
// # while ((InternetReadFile(request, buf, BUF_MAX, &nr_read)) && nr_read > 0)
for
{
println('111')
ok := C.InternetReadFile(request, buf, BUF_MAX, &nr_read)
println('222')
if !ok {
println('InternetReadFile() not ok ')
}
if ok && nr_read == 0 {
println('ok && nr read == 0, breaking')
C.printf('buf broken="%s"\n', buf)
//println('ok && nr read == 0, breaking')
//C.printf('buf broken="%s"\n', buf)
if req.url.contains('websocket') {
println('win sleeping 2')
time.sleep(2)
@ -153,10 +153,11 @@ fn (req &Request) do() Response {
}
break
}
println('ireadfile()')
//println('ireadfile()')
buf[nr_read] = 0
C.printf('buf="%s"\n', buf)
s += string(buf)// TODO perf
//C.printf('buf="%s"\n', buf)
s += tos(buf, nr_read + 1) // TODO perf
nr_read = 0
}
C.InternetCloseHandle(request)
@ -174,16 +175,16 @@ fn (req &Request) do() Response {
// println('\n!')
// println(h)
vals := h.split(':')
pos := h.index(':')
hpos := h.index(':')
if pos == -1 {
continue
}
key := h.left(pos)
val := h.right(pos + 1)
key := h.left(hpos)
val := h.right(hpos + 1)
// println('$key => $val')
resp.headers[key] = val.trim_space()
}
println('END OF WIN req.do($req.url)')
//println('END OF WIN req.do($req.url)')
return resp
}