aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bindings/curl.nim8
-rw-r--r--src/loader/connecterror.nim1
-rw-r--r--src/loader/curlhandle.nim32
-rw-r--r--src/loader/curlwrap.nim10
-rw-r--r--src/loader/ftp.nim165
-rw-r--r--src/loader/http.nim41
-rw-r--r--src/loader/loader.nim8
7 files changed, 227 insertions, 38 deletions
diff --git a/src/bindings/curl.nim b/src/bindings/curl.nim
index bab180f5..825ab656 100644
--- a/src/bindings/curl.nim
+++ b/src/bindings/curl.nim
@@ -79,9 +79,11 @@ type
CURLOPT_SSLVERSION = CURLOPTTYPE_VALUES + 32
CURLOPT_TIMECONDITION = CURLOPTTYPE_VALUES + 33
CURLOPT_POST = CURLOPTTYPE_LONG + 47
+ CURLOPT_DIRLISTONLY = CURLOPTTYPE_LONG + 48
CURLOPT_FOLLOWLOCATION = CURLOPTTYPE_LONG + 52
CURLOPT_POSTFIELDSIZE = CURLOPTTYPE_LONG + 60
CURLOPT_HTTPGET = CURLOPTTYPE_LONG + 80
+ CURLOPT_FTP_FILEMETHOD = CURLOPTTYPE_VALUES + 138
CURLOPT_CONNECT_ONLY = CURLOPTTYPE_LONG + 141
# Objectpoint
@@ -265,6 +267,12 @@ type
CURLE_UNRECOVERABLE_POLL, # 99 - poll/select returned fatal error
CURL_LAST # never use!
+ curl_ftpmethod* {.size: sizeof(cint).} = enum
+ CURLFTPMETHOD_DEFAULT, # let libcurl pick
+ CURLFTPMETHOD_MULTICWD, # single CWD operation for each path part
+ CURLFTPMETHOD_NOCWD, # no CWD at all
+ CURLFTPMETHOD_SINGLECWD, # one CWD to full dir, then work on file
+
CURLMcode* {.size: sizeof(cint).} = enum
CURLM_CALL_MULTI_PERFORM = -1, # please call curl_multi_perform() or
# curl_multi_socket*() soon
diff --git a/src/loader/connecterror.nim b/src/loader/connecterror.nim
index d2af5762..5fce5287 100644
--- a/src/loader/connecterror.nim
+++ b/src/loader/connecterror.nim
@@ -1,6 +1,7 @@
import bindings/curl
type ConnectErrorCode* = enum
+ ERROR_INVALID_METHOD = (-8, "invalid method")
ERROR_INVALID_DATA_URL = (-7, "invalid data URL")
ERROR_ABOUT_PAGE_NOT_FOUND = (-6, "about page not found")
ERROR_FILE_NOT_FOUND = (-5, "file not found")
diff --git a/src/loader/curlhandle.nim b/src/loader/curlhandle.nim
new file mode 100644
index 00000000..3c69c6c0
--- /dev/null
+++ b/src/loader/curlhandle.nim
@@ -0,0 +1,32 @@
+import bindings/curl
+import loader/headers
+import loader/loaderhandle
+import loader/request
+
+type
+ CurlHandle* = ref object of RootObj
+ curl*: CURL
+ statusline*: bool
+ headers*: Headers
+ request*: Request
+ handle*: LoaderHandle
+ mime*: curl_mime
+ slist*: curl_slist
+ finish*: proc(handle: CurlHandle)
+
+func newCurlHandle*(curl: CURL, request: Request, handle: LoaderHandle):
+ CurlHandle =
+ return CurlHandle(
+ headers: newHeaders(),
+ curl: curl,
+ handle: handle,
+ request: request
+ )
+
+proc cleanup*(handleData: CurlHandle) =
+ handleData.handle.close()
+ if handleData.mime != nil:
+ curl_mime_free(handleData.mime)
+ if handleData.slist != nil:
+ curl_slist_free_all(handleData.slist)
+ curl_easy_cleanup(handleData.curl)
diff --git a/src/loader/curlwrap.nim b/src/loader/curlwrap.nim
new file mode 100644
index 00000000..7aef4182
--- /dev/null
+++ b/src/loader/curlwrap.nim
@@ -0,0 +1,10 @@
+import bindings/curl
+
+template setopt*(curl: CURL, opt: CURLoption, arg: typed) =
+ discard curl_easy_setopt(curl, opt, arg)
+
+template setopt*(curl: CURL, opt: CURLoption, arg: string) =
+ discard curl_easy_setopt(curl, opt, cstring(arg))
+
+template getinfo*(curl: CURL, info: CURLINFO, arg: typed) =
+ discard curl_easy_getinfo(curl, info, arg)
diff --git a/src/loader/ftp.nim b/src/loader/ftp.nim
new file mode 100644
index 00000000..9aa4c88b
--- /dev/null
+++ b/src/loader/ftp.nim
@@ -0,0 +1,165 @@
+import strutils
+
+import bindings/curl
+import loader/connecterror
+import loader/curlhandle
+import loader/curlwrap
+import loader/headers
+import loader/loaderhandle
+import loader/request
+import types/opt
+import types/url
+import utils/twtstr
+
+type FtpHandle = ref object of CurlHandle
+ buffer: string
+ dirmode: bool
+ base: string
+ path: string
+
+func newFtpHandle(curl: CURL, request: Request, handle: LoaderHandle,
+ dirmode: bool): FtpHandle =
+ return FtpHandle(
+ headers: newHeaders(),
+ curl: curl,
+ handle: handle,
+ request: request,
+ dirmode: dirmode
+ )
+
+proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t,
+ userdata: pointer): csize_t {.cdecl.} =
+ var line = newString(nitems)
+ if nitems > 0:
+ copyMem(addr line[0], addr p[0], nitems)
+
+ let op = cast[FtpHandle](userdata)
+
+ if not op.statusline:
+ op.statusline = true
+ if not op.handle.sendResult(int(CURLE_OK)):
+ return 0
+ var status: clong
+ op.curl.getinfo(CURLINFO_RESPONSE_CODE, addr status)
+ if not op.handle.sendStatus(cast[int](status)):
+ return 0
+ if op.dirmode:
+ op.headers.add("Content-Type", "text/html")
+ if not op.handle.sendHeaders(op.headers):
+ return 0
+ if op.dirmode:
+ if not op.handle.sendData("""
+<HTML>
+<HEAD>
+<BASE HREF=""" & op.base & """>
+<TITLE>""" & op.path & """</TITLE>
+</HEAD>
+<BODY>
+<H1>Index of """ & op.path & """</H1>
+<PRE>
+<A HREF="..">
+[Upper Directory]</A>"""):
+ return 0
+ return nitems
+ return nitems
+
+# From the documentation: size is always 1.
+proc curlWriteBody(p: cstring, size: csize_t, nmemb: csize_t,
+ userdata: pointer): csize_t {.cdecl.} =
+ let op = cast[FtpHandle](userdata)
+
+ if nmemb > 0:
+ if op.dirmode:
+ let i = op.buffer.len
+ op.buffer.setLen(op.buffer.len + int(nmemb))
+ copyMem(addr op.buffer[i], addr p[0], nmemb)
+ else:
+ if not op.handle.sendData(p, int(nmemb)):
+ return 0
+ return nmemb
+
+proc finish(op: CurlHandle) =
+ let op = cast[FtpHandle](op)
+ for line in op.buffer.split('\n'):
+ var i = 10 # permission
+ template skip_till_space =
+ while i < line.len and line[i] != ' ':
+ inc i
+ # link count
+ i = line.skipBlanks(i)
+ while i < line.len and line[i] in AsciiDigit:
+ inc i
+ # owner
+ i = line.skipBlanks(i)
+ skip_till_space
+ # group
+ i = line.skipBlanks(i)
+ while i < line.len and line[i] != ' ':
+ inc i
+ # size
+ i = line.skipBlanks(i)
+ var sizes = ""
+ while i < line.len and line[i] in AsciiDigit:
+ sizes &= line[i]
+ inc i
+ let nsize = parseInt64(sizes).get(-1)
+ # date
+ i = line.skipBlanks(i)
+ skip_till_space # m
+ i = line.skipBlanks(i)
+ skip_till_space # d
+ i = line.skipBlanks(i)
+ skip_till_space # y
+ inc i
+ let name = line.substr(i)
+ case line[0]
+ of 'l': # link
+ let x = " -> "
+ let linki = name.find(x)
+ let linkfrom = name.substr(0, linki - 1)
+ let linkto = name.substr(linki + 4) # you?
+ discard op.handle.sendData("""
+<A HREF="""" & linkfrom & """"">
+""" & name & """@ (-> """ & linkto & """)</A>""")
+ of 'd': # directory
+ discard op.handle.sendData("""
+<A HREF="""" & name & """/">
+""" & name & """/</A>""")
+ else: # file
+ discard op.handle.sendData("""
+<A HREF="""" & name & """">
+""" & name & """ (""" & $nsize & """)</A>""")
+ discard op.handle.sendData("""
+</PRE>
+</BODY>
+</HTML>
+""")
+
+proc loadFtp*(handle: LoaderHandle, curlm: CURLM,
+ request: Request): CurlHandle =
+ let curl = curl_easy_init()
+ doAssert curl != nil
+ let surl = request.url.serialize()
+ curl.setopt(CURLOPT_URL, surl)
+ let path = request.url.path.serialize_unicode()
+ let dirmode = path.len > 0 and path[^1] == '/'
+ let handleData = curl.newFtpHandle(request, handle, dirmode)
+ curl.setopt(CURLOPT_HEADERDATA, handleData)
+ curl.setopt(CURLOPT_HEADERFUNCTION, curlWriteHeader)
+ curl.setopt(CURLOPT_WRITEDATA, handleData)
+ curl.setopt(CURLOPT_WRITEFUNCTION, curlWriteBody)
+ if dirmode:
+ handleData.finish = finish
+ handleData.base = surl
+ handleData.path = path
+ if request.proxy != nil:
+ let purl = request.proxy.serialize()
+ curl.setopt(CURLOPT_PROXY, purl)
+ if request.httpmethod != HTTP_GET:
+ discard handle.sendResult(int(ERROR_INVALID_METHOD))
+ return nil
+ let res = curl_multi_add_handle(curlm, curl)
+ if res != CURLM_OK:
+ discard handle.sendResult(int(res))
+ return nil
+ return handleData
diff --git a/src/loader/http.nim b/src/loader/http.nim
index 97fd9690..a767dd13 100644
--- a/src/loader/http.nim
+++ b/src/loader/http.nim
@@ -2,52 +2,17 @@ import options
import strutils
import bindings/curl
+import loader/curlhandle
+import loader/curlwrap
import loader/headers
import loader/loaderhandle
import loader/request
import types/blob
import types/formdata
-import types/url
import types/opt
+import types/url
import utils/twtstr
-type
- CurlHandle* = ref CurlHandleObj
- CurlHandleObj = object
- curl*: CURL
- statusline: bool
- headers: Headers
- request: Request
- handle*: LoaderHandle
- mime: curl_mime
- slist: curl_slist
-
-func newCurlHandle(curl: CURL, request: Request, handle: LoaderHandle):
- CurlHandle =
- return CurlHandle(
- headers: newHeaders(),
- curl: curl,
- handle: handle,
- request: request
- )
-
-proc cleanup*(handleData: CurlHandle) =
- handleData.handle.close()
- if handleData.mime != nil:
- curl_mime_free(handleData.mime)
- if handleData.slist != nil:
- curl_slist_free_all(handleData.slist)
- curl_easy_cleanup(handleData.curl)
-
-template setopt(curl: CURL, opt: CURLoption, arg: typed) =
- discard curl_easy_setopt(curl, opt, arg)
-
-template setopt(curl: CURL, opt: CURLoption, arg: string) =
- discard curl_easy_setopt(curl, opt, cstring(arg))
-
-template getinfo(curl: CURL, info: CURLINFO, arg: typed) =
- discard curl_easy_getinfo(curl, info, arg)
-
proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t,
userdata: pointer): csize_t {.cdecl.} =
var line = newString(nitems)
diff --git a/src/loader/loader.nim b/src/loader/loader.nim
index 162dd120..7ccd9a7f 100644
--- a/src/loader/loader.nim
+++ b/src/loader/loader.nim
@@ -30,8 +30,10 @@ import js/error
import js/javascript
import loader/about
import loader/connecterror
+import loader/curlhandle
import loader/data
import loader/file
+import loader/ftp
import loader/headers
import loader/http
import loader/loaderhandle
@@ -113,6 +115,10 @@ proc loadResource(ctx: LoaderContext, request: Request, handle: LoaderHandle) =
of "data":
handle.loadData(request)
handle.close()
+ of "ftp", "ftps", "sftp":
+ let handleData = handle.loadFtp(ctx.curlm, request)
+ if handleData != nil:
+ ctx.handleList.add(handleData)
else:
discard handle.sendResult(ERROR_UNKNOWN_SCHEME)
handle.close()
@@ -164,6 +170,8 @@ proc acceptConnection(ctx: LoaderContext) =
proc finishCurlTransfer(ctx: LoaderContext, handleData: CurlHandle, res: int) =
if res != int(CURLE_OK):
discard handleData.handle.sendResult(int(res))
+ if handleData.finish != nil:
+ handleData.finish(handleData)
discard curl_multi_remove_handle(ctx.curlm, handleData.curl)
handleData.cleanup()