about summary refs log tree commit diff stats
path: root/src/loader/ftp.nim
blob: 9aa4c88bc3d0d42e15ba516b0acacbc155dfed6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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