aboutsummaryrefslogtreecommitdiff
path: root/libbutl/curl.cxx
blob: 92baa4c9f9e11d0ab37136c8ee8e7962ccc28e35 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// file      : libbutl/curl.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbutl/curl.hxx>

#include <cassert>
#include <utility>   // move()
#include <cstdlib>   // strtoul(), size_t
#include <exception> // invalid_argument

#include <libbutl/utility.hxx>

using namespace std;

namespace butl
{
  process::pipe curl::
  map_in (nullfd_t, method_proto mp, io_data& d)
  {
    switch (mp)
    {
    case ftp_put:
      throw invalid_argument ("no input specified for PUT method");
    case http_post:
      throw invalid_argument ("no input specified for POST method");
    case ftp_get:
    case http_get:
      {
        d.pipe.in = fdopen_null (); // /dev/null
        return pipe (d.pipe);
      }
    }

    assert (false); // Can't be here.
    return pipe ();
  }

  process::pipe curl::
  map_in (const path& f, method_proto mp, io_data& d)
  {
    pipe r;
    switch (mp)
    {
    case ftp_put:
    case http_post:
      {
        if (mp == ftp_put)
        {
          d.options.push_back ("--upload-file");
          d.options.push_back (f.string ().c_str ());
        }
        else
        {
          d.storage = '@' + f.string ();

          d.options.push_back ("--data-binary");
          d.options.push_back (d.storage.c_str ());
        }

        if (f.string () == "-")
        {
          d.pipe = fdopen_pipe (fdopen_mode::binary);
          r = pipe (d.pipe);

          out.open (move (d.pipe.out));
        }
        else
        {
          d.pipe.in = fdopen_null (); // /dev/null
          r = pipe (d.pipe);
        }

        return r;
      }
    case ftp_get:
    case http_get:
      {
        throw invalid_argument ("file input specified for GET method");
      }
    }

    assert (false); // Can't be here.
    return r;
  }

  process::pipe curl::
  map_out (nullfd_t, method_proto mp, io_data& d)
  {
    switch (mp)
    {
    case ftp_get:
    case http_get:
      throw invalid_argument ("no output specified for GET method");
    case ftp_put:
    case http_post: // May or may not produce output.
      {
        d.pipe.out = fdopen_null ();
        return pipe (d.pipe); // /dev/null
      }
    }

    assert (false); // Can't be here.
    return pipe ();
  }

  process::pipe curl::
  map_out (const path& f, method_proto mp, io_data& d)
  {
    pipe r;
    switch (mp)
    {
    case ftp_get:
    case http_get:
    case http_post:
      {
        if (f.string () == "-")
        {
          // Note: no need for any options, curl writes to stdout by default.
          //
          d.pipe = fdopen_pipe (fdopen_mode::binary);
          r = pipe (d.pipe);

          in.open (move (d.pipe.in));
        }
        else
        {
          d.options.push_back ("-o");
          d.options.push_back (f.string ().c_str ());
          d.pipe.out = fdopen_null (); // /dev/null
          r = pipe (d.pipe);
        }

        return r;
      }
    case ftp_put:
      {
        throw invalid_argument ("file output specified for PUT method");
      }
    }

    assert (false); // Can't be here.
    return r;
  }

  curl::method_proto curl::
  translate (method_type m, const string& u, method_proto_options& o)
  {
    size_t n (u.find ("://"));

    if (n == string::npos)
      throw invalid_argument ("no protocol in URL");

    if (icasecmp (u, "ftp", n) == 0 || icasecmp (u, "tftp", n) == 0)
    {
      switch (m)
      {
      case method_type::get: return method_proto::ftp_get;
      case method_type::put: return method_proto::ftp_put;
      case method_type::post:
        throw invalid_argument ("POST method with FTP protocol");
      }
    }
    else if (icasecmp (u, "http", n) == 0 || icasecmp (u, "https", n) == 0)
    {
      o.push_back ("--fail");     // Fail on HTTP errors (e.g., 404).
      o.push_back ("--location"); // Follow redirects.

      switch (m)
      {
      case method_type::get:  return method_proto::http_get;
      case method_type::post: return method_proto::http_post;
      case method_type::put:
        throw invalid_argument ("PUT method with HTTP protocol");
      }
    }

    throw invalid_argument ("unsupported protocol");
  }

  uint16_t curl::
  parse_http_status_code (const string& s)
  {
    char* e (nullptr);
    unsigned long c (strtoul (s.c_str (), &e, 10)); // Can't throw.
    assert (e != nullptr);

    return *e == '\0' && c >= 100 && c < 600
           ? static_cast<uint16_t> (c)
           : 0;
  }

  string curl::
  read_http_response_line (ifdstream& is)
  {
    string r;
    getline (is, r); // Strips the trailing LF (0xA).

    // Note that on POSIX CRLF is not automatically translated into LF, so we
    // need to strip CR (0xD) manually.
    //
    if (!r.empty () && r.back () == '\r')
      r.pop_back ();

    return r;
  }

  curl::http_status curl::
  read_http_status (ifdstream& is, bool skip_headers)
  {
    // After getting the status line, if requested, we will read until the
    // empty line (containing just CRLF). Not being able to reach such a line
    // is an error, which is the reason for the exception mask choice. When
    // done, we will restore the original exception mask.
    //
    ifdstream::iostate es (is.exceptions ());
    is.exceptions (ifdstream::badbit | ifdstream::failbit | ifdstream::eofbit);

    auto read_status = [&is, es] ()
    {
      string l (read_http_response_line (is));

      for (;;) // Breakout loop.
      {
        if (l.compare (0, 5, "HTTP/") != 0)
          break;

        size_t p (l.find (' ', 5));             // The protocol end.
        if (p == string::npos)
          break;

        p = l.find_first_not_of (' ', p + 1);   // The code start.
        if (p == string::npos)
          break;

        size_t e (l.find (' ', p + 1));         // The code end.
        if (e == string::npos)
          break;

        uint16_t c (parse_http_status_code (string (l, p, e - p)));
        if (c == 0)
          break;

        string r;
        p = l.find_first_not_of (' ', e + 1);   // The reason start.
        if (p != string::npos)
        {
          e = l.find_last_not_of (' ');         // The reason end.
          assert (e != string::npos && e >= p);

          r = string (l, p, e - p + 1);
        }

        return http_status {c, move (r)};
      }

      is.exceptions (es); // Restore the exception mask.

      throw invalid_argument ("invalid status line '" + l + "'");
    };

    // The curl output for a successfull request looks like this:
    //
    // HTTP/1.1 100 Continue
    //
    // HTTP/1.1 200 OK
    // Content-Length: 83
    // Content-Type: text/manifest;charset=utf-8
    //
    // <response-body>
    //
    // curl normally sends the 'Expect: 100-continue' header for uploads, so
    // we need to handle the interim HTTP server response with the continue
    // (100) status code.
    //
    // Interestingly, Apache can respond with the continue (100) code and with
    // the not found (404) code afterwords.
    //
    http_status rs (read_status ());

    if (rs.code == 100)
    {
      // Skips the interim response.
      //
      while (!read_http_response_line (is).empty ()) ;

      rs = read_status (); // Reads the final status code.
    }

    if (skip_headers)
    {
      while (!read_http_response_line (is).empty ()) ; // Skips headers.
    }

    is.exceptions (es);

    return rs;
  }
}