~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/curl_io_handler.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2009-04-22 21:39:19 UTC
  • mto: (4.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20090422213919-52m015y6gcpv1m1g
Tags: upstream-0.12.0~svn2018
ImportĀ upstreamĀ versionĀ 0.12.0~svn2018

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
9
9
    
10
 
    Copyright (C) 2006-2008 Gena Batyan <bgeradz@mediatomb.cc>,
 
10
    Copyright (C) 2006-2009 Gena Batyan <bgeradz@mediatomb.cc>,
11
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
12
                            Leonhard Wimmer <leo@mediatomb.cc>
13
13
    
24
24
    version 2 along with MediaTomb; if not, write to the Free Software
25
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26
26
    
27
 
    $Id: curl_io_handler.cc 1698 2008-02-23 20:48:30Z lww $
 
27
    $Id: curl_io_handler.cc 2010 2009-01-11 19:10:43Z lww $
28
28
*/
29
29
 
30
30
/// \file curl_io_handler.cc
35
35
 
36
36
#ifdef HAVE_CURL
37
37
 
 
38
#include "config_manager.h"
38
39
#include "tools.h"
39
40
#include "curl_io_handler.h"
40
41
 
52
53
    this->curl_handle = curl_handle;
53
54
    //bytesCurl = 0;
54
55
    signalAfterEveryRead = true;
 
56
    
 
57
    // still todo:
 
58
    // * optimize seek if data already in buffer
 
59
    seekEnabled = true;
55
60
}
56
61
 
57
62
void CurlIOHandler::open(IN enum UpnpOpenFileMode mode)
58
63
{
59
 
    
60
64
    if (curl_handle == NULL)
61
65
    {
62
66
        curl_handle = curl_easy_init();
90
94
    curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
91
95
    curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
92
96
    curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, -1);
 
97
    
 
98
    bool logEnabled;
 
99
#ifdef TOMBDEBUG
 
100
    logEnabled = !ConfigManager::isDebugLogging();
 
101
#else
 
102
    logEnabled = ConfigManager::isDebugLogging();
 
103
#endif
 
104
    if (logEnabled)
 
105
        curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
 
106
    
93
107
    //curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT,
94
108
    
95
109
    //proxy..
97
111
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, CurlIOHandler::curlCallback);
98
112
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)this);
99
113
    
100
 
    res = curl_easy_perform(curl_handle);
 
114
    AUTOLOCK_NOLOCK(mutex);
 
115
    do
 
116
    {
 
117
        AUTORELOCK();
 
118
        if (doSeek)
 
119
        {
 
120
            log_debug("SEEK: %lld %d\n", seekOffset, seekWhence);
 
121
            
 
122
            if (seekWhence == SEEK_SET)
 
123
            {
 
124
                posRead = seekOffset;
 
125
                curl_easy_setopt(curl_handle, CURLOPT_RESUME_FROM_LARGE, seekOffset);
 
126
            }
 
127
            else if (seekWhence == SEEK_CUR)
 
128
            {
 
129
                posRead += seekOffset;
 
130
                curl_easy_setopt(curl_handle, CURLOPT_RESUME_FROM_LARGE, posRead);
 
131
            }
 
132
            else
 
133
            {
 
134
                log_error("CurlIOHandler currently does not support SEEK_END\n");
 
135
                assert(1);
 
136
            }
 
137
            
 
138
            /// \todo should we do that?
 
139
            waitForInitialFillSize = (initialFillSize > 0);
 
140
            
 
141
            doSeek = false;
 
142
            cond->signal();
 
143
        }
 
144
        AUTOUNLOCK();
 
145
        res = curl_easy_perform(curl_handle);
 
146
    }
 
147
    while (doSeek);
 
148
    
101
149
    if (res != CURLE_OK)
102
150
        readError = true;
103
151
    else
124
172
    int bufFree = 0;
125
173
    do
126
174
    {
 
175
        if (ego->doSeek && ! ego->empty && 
 
176
                (
 
177
                    ego->seekWhence == SEEK_SET ||
 
178
                    (ego->seekWhence == SEEK_CUR && ego->seekOffset > 0)
 
179
                )
 
180
            )
 
181
        {
 
182
            int currentFillSize = ego->b - ego->a;
 
183
            if (currentFillSize <= 0)
 
184
                currentFillSize += ego->bufSize;
 
185
            
 
186
            int relSeek = ego->seekOffset;
 
187
            if (ego->seekWhence == SEEK_SET)
 
188
                relSeek -= ego->posRead;
 
189
            
 
190
            if (relSeek <= currentFillSize)
 
191
            { // we have everything we need in the buffer already
 
192
                ego->a += relSeek;
 
193
                ego->posRead += relSeek;
 
194
                if (ego->a >= ego->bufSize)
 
195
                    ego->a -= ego->bufSize;
 
196
                if (ego->a == ego->b)
 
197
                {
 
198
                    ego->empty = true;
 
199
                    ego->a = ego->b = 0;
 
200
                }
 
201
                /// \todo do we need to wait for initialFillSize again?
 
202
            
 
203
                ego->doSeek = false;
 
204
                ego->cond->signal();
 
205
            }
 
206
        }
 
207
        
 
208
        // note: seeking could be optimized some more (backward seeking)
 
209
        // but this should suffice for now
 
210
        
 
211
        if (ego->doSeek)
 
212
        { // seek not been processed yet
 
213
            ego->a = ego->b = 0;
 
214
            ego->empty = true;
 
215
            
 
216
            // terminate this request, because we need a new request
 
217
            // after the seek
 
218
            return 0;
 
219
        }
 
220
        
127
221
        if (! first)
128
222
        {
129
223
            ego->cond->wait();
163
257
    
164
258
    AUTORELOCK();
165
259
    
 
260
    //ego->bytesCurl += wantWrite;
166
261
    ego->b += wantWrite;
167
262
    if (ego->b >= ego->bufSize)
168
263
        ego->b -= ego->bufSize;
184
279
        }
185
280
    }
186
281
    
187
 
    //ego->bytesCurl += wantWrite;
188
282
    return wantWrite;
189
283
}
190
284