summaryrefslogtreecommitdiffstats
path: root/src/url_loader.hh
blob: 799d1a23d468516971725a33b45e02e65a907753 (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
/**
 * Copyright (c) 2015, Timothy Stack
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither the name of Timothy Stack nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef url_loader_hh
#define url_loader_hh

#include "config.h"

#ifdef HAVE_LIBCURL
#    include <curl/curl.h>
#    include <paths.h>

#    include "base/fs_util.hh"
#    include "curl_looper.hh"

class url_loader : public curl_request {
public:
    url_loader(const std::string& url) : curl_request(url)
    {
        auto tmp_res = lnav::filesystem::open_temp_file(
            ghc::filesystem::temp_directory_path() / "lnav.url.XXXXXX");
        if (tmp_res.isErr()) {
            return;
        }

        auto tmp_pair = tmp_res.unwrap();
        ghc::filesystem::remove(tmp_pair.first);
        this->ul_fd = std::move(tmp_pair.second);

        curl_easy_setopt(this->cr_handle, CURLOPT_URL, this->cr_name.c_str());
        curl_easy_setopt(this->cr_handle, CURLOPT_WRITEFUNCTION, write_cb);
        curl_easy_setopt(this->cr_handle, CURLOPT_WRITEDATA, this);
        curl_easy_setopt(this->cr_handle, CURLOPT_FILETIME, 1);
        curl_easy_setopt(this->cr_handle, CURLOPT_BUFFERSIZE, 128L * 1024L);
    }

    int get_fd() const { return this->ul_fd.get(); }

    auto_fd copy_fd() const { return this->ul_fd.dup(); }

    long complete(CURLcode result)
    {
        curl_request::complete(result);

        switch (result) {
            case CURLE_OK:
                break;
            case CURLE_BAD_DOWNLOAD_RESUME:
                break;
            default:
                log_error("%s:curl failure -- %ld %s",
                          this->cr_name.c_str(),
                          result,
                          curl_easy_strerror(result));
                log_perror(write(this->ul_fd,
                                 this->cr_error_buffer,
                                 strlen(this->cr_error_buffer)));
                return -1;
        }

        long file_time;
        CURLcode rc;

        rc = curl_easy_getinfo(this->cr_handle, CURLINFO_FILETIME, &file_time);
        if (rc == CURLE_OK) {
            time_t current_time;

            time(&current_time);
            if (file_time == -1
                || (current_time - file_time) < FOLLOW_IF_MODIFIED_SINCE) {
                char range[64];
                struct stat st;
                off_t start;

                fstat(this->ul_fd, &st);
                if (st.st_size > 0) {
                    start = st.st_size - 1;
                    this->ul_resume_offset = 1;
                } else {
                    start = 0;
                    this->ul_resume_offset = 0;
                }
                snprintf(range, sizeof(range), "%ld-", (long) start);
                curl_easy_setopt(this->cr_handle, CURLOPT_RANGE, range);
                return 2000;
            } else {
                log_debug("URL was not recently modified, not tailing: %s",
                          this->cr_name.c_str());
            }
        } else {
            log_error("Could not get file time for URL: %s -- %s",
                      this->cr_name.c_str(),
                      curl_easy_strerror(rc));
        }

        return -1;
    }

private:
    static const long FOLLOW_IF_MODIFIED_SINCE = 60 * 60;

    static ssize_t write_cb(void* contents,
                            size_t size,
                            size_t nmemb,
                            void* userp)
    {
        url_loader* ul = (url_loader*) userp;
        char* c_contents = (char*) contents;
        ssize_t retval;

        c_contents += ul->ul_resume_offset;
        retval = write(
            ul->ul_fd, c_contents, (size * nmemb) - ul->ul_resume_offset);
        retval += ul->ul_resume_offset;
        ul->ul_resume_offset = 0;
        return retval;
    }

    auto_fd ul_fd;
    off_t ul_resume_offset{0};
};
#endif

#endif