Statistics
| Branch: | Revision:

chunker-player / chunk_pusher_curl.c @ 63c21257

History | View | Annotate | Download (1.59 KB)

1
#include <stdint.h>
2
#include <curl/curl.h>
3

    
4
#include <chunk.h>
5

    
6
#include "chunker_streamer.h"
7

    
8

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

    
13

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

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

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

    
29
        int ret = STREAMER_FAIL_RETURN;
30

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