Statistics
| Branch: | Revision:

chunker-player / chunk_pusher_curl.c @ 64144d8f

History | View | Annotate | Download (1.57 KB)

1
#include <curl/curl.h>
2

    
3
#include <chunk.h>
4

    
5
#include "chunker_streamer.h"
6

    
7

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

    
12

    
13
void initChunkPusher() {        
14
        /* In windows, this will init the winsock stuff */ 
15
        curl_global_init(CURL_GLOBAL_ALL);
16
}
17

    
18
void finalizeChunkPusher() {
19
        curl_global_cleanup();
20
}
21

    
22
int sendViaCurl(Chunk gchunk, int buffer_size, char *url) {
23
        //MAKE THE CURL EASY HANDLE GLOBAL? TO REUSE IT?
24
        CURL *curl_handle;
25
        struct curl_slist *headers=NULL;
26
        uint8_t *buffer=NULL;
27

    
28
        int ret = STREAMER_FAIL_RETURN;
29

    
30
        if( (buffer = malloc(buffer_size)) != NULL) {
31
                /* encode the GRAPES chunk into network bytes */
32
                encodeChunk(&gchunk, buffer, buffer_size);
33
                /* get a curl handle */ 
34
                curl_handle = curl_easy_init();
35
                if(curl_handle) {
36
                        curl_easy_setopt(curl_handle, CURLOPT_URL, url);
37
                        /* fill the headers */
38
                        headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
39
                        /* disable Expect: header */
40
                        headers = curl_slist_append(headers, "Expect:");
41
                        /* post binary data */
42
                        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, buffer);
43
                        /* set the size of the postfields data */
44
                        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, buffer_size);
45
                        /* pass our list of custom made headers */
46
                        curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
47
                        curl_easy_perform(curl_handle); /* post away! */
48
                        curl_slist_free_all(headers); /* free the header list */
49
                        ret = STREAMER_OK_RETURN;
50
                }
51
                /* always cleanup curl */ 
52
                curl_easy_cleanup(curl_handle);
53
                free(buffer);
54
        }
55
        return ret;
56
}