Statistics
| Branch: | Revision:

chunker-player / chunker_streamer / chunk_pusher_curl.c @ e11386c0

History | View | Annotate | Download (2.02 KB)

1
#define CURL_STATICLIB
2
#include <curl/curl.h>
3

    
4
#include "streamer_commons.h"
5

    
6
void initChunkPusher();
7
void finalizeChunkPusher();
8
int sendViaCurl(Chunk gchunk, int buffer_size, char *url);
9

    
10
//MAKE THE CURL EASY HANDLE GLOBAL TO REUSE IT CONNECTIONS
11
CURL *curl_handle = 0;
12

    
13
void initChunkPusher() {
14
        /* In windows, this will init the winsock stuff */ 
15
        curl_global_init(CURL_GLOBAL_ALL);
16
        /* get a curl handle */ 
17
        curl_handle = curl_easy_init();
18
        fprintf(stderr, "CURL client initialized with handle %p\n", curl_handle);
19
}
20

    
21
void finalizeChunkPusher() {
22
        /* always cleanup curl */ 
23
        curl_easy_cleanup(curl_handle);
24
        fprintf(stderr, "CURL client finalized handle %p\n", curl_handle);
25
        curl_global_cleanup();
26
}
27

    
28
int sendViaCurl(Chunk gchunk, int buffer_size, char *url) {
29
        struct curl_slist *headers=NULL;
30
        uint8_t *buffer=NULL;
31

    
32
        int ret = STREAMER_FAIL_RETURN;
33

    
34
        if( (buffer = malloc(buffer_size)) != NULL) {
35
                /* encode the GRAPES chunk into network bytes */
36
                encodeChunk(&gchunk, buffer, buffer_size);
37

    
38
                if(curl_handle) {
39
                        curl_easy_setopt(curl_handle, CURLOPT_URL, url);
40
                        /* fill the headers */
41
                        headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
42
                        /* disable Expect: header */
43
                        headers = curl_slist_append(headers, "Expect:");
44
                        /* enable Connection: keep-alive */
45
                        //headers = curl_slist_append(headers, "Connection: keep-alive");
46
                        /* enable chunked */
47
                        //headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
48
                        /* force HTTP 1.0 */
49
                        //curl_easy_setopt (curl_handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
50
                        /* post binary data */
51
                        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, buffer);
52
                        /* set the size of the postfields data */
53
                        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, buffer_size);
54
                        /* pass our list of custom made headers */
55
                        curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
56
                        curl_easy_perform(curl_handle); /* post away! */
57
                        curl_slist_free_all(headers); /* free the header list */
58
                        ret = STREAMER_OK_RETURN;
59
                }
60

    
61
                free(buffer);
62
        }
63
        return ret;
64
}