diff options
Diffstat (limited to 'libpiano/src/http.c')
-rw-r--r-- | libpiano/src/http.c | 91 |
1 files changed, 18 insertions, 73 deletions
diff --git a/libpiano/src/http.c b/libpiano/src/http.c index 6a73aa7..acae893 100644 --- a/libpiano/src/http.c +++ b/libpiano/src/http.c @@ -21,40 +21,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include <curl/curl.h> #include <stdlib.h> #include <string.h> +#include <stdio.h> -#include "http.h" - -struct PianoHttpBuffer { - size_t size; /* size of string without NUL-byte */ - char *buf; /* NUL-terminated string */ -}; - -/* callback for curl, writes data to buffer - * @param received data - * @param block size - * @param blocks received - * @param write data into this buffer - * @return written bytes - */ -size_t PianoCurlRetToVar (void *ptr, size_t size, size_t nmemb, - void *stream) { - struct PianoHttpBuffer *curlRet = stream; +#include <waitress.h> - if (curlRet->buf == NULL) { - curlRet->buf = malloc (nmemb + 1); - curlRet->size = 0; - } else { - curlRet->buf = realloc (curlRet->buf, curlRet->size + nmemb + 1); - } - memcpy (curlRet->buf + curlRet->size, ptr, size*nmemb); - curlRet->size += nmemb; - curlRet->buf[curlRet->size] = 0; - - return size*nmemb; -} +#include "http.h" /* FIXME: we may use a callback given by the library client here. would be * more flexible... */ @@ -65,33 +38,16 @@ size_t PianoCurlRetToVar (void *ptr, size_t size, size_t nmemb, * @param put received data here, memory is allocated by this function * @return nothing yet */ -PianoReturn_t PianoHttpPost (CURL *ch, const char *url, const char *postData, - char **retData) { - struct curl_slist *headers = NULL; - struct PianoHttpBuffer curlRet = {0, NULL}; - PianoReturn_t ret; - - headers = curl_slist_append (headers, "Content-Type: text/xml"); - - curl_easy_setopt (ch, CURLOPT_URL, url); - curl_easy_setopt (ch, CURLOPT_POSTFIELDS, postData); - curl_easy_setopt (ch, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt (ch, CURLOPT_WRITEFUNCTION, PianoCurlRetToVar); - /* don't verify certificate for now, it's easier ;) */ - curl_easy_setopt (ch, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt (ch, CURLOPT_WRITEDATA, (void *) &curlRet); - - if (curl_easy_perform (ch) == CURLE_OK) { - ret = PIANO_RET_OK; - *retData = curlRet.buf; - } else { - ret = PIANO_RET_NET_ERROR; - *retData = NULL; +PianoReturn_t PianoHttpPost (WaitressHandle_t *waith, const char *postData, + char *retData, size_t retDataSize) { + waith->extraHeaders = "Content-Type: text/xml\r\n"; + waith->postData = postData; + waith->method = WAITRESS_METHOD_POST; + + if (WaitressFetchBuf (waith, retData, retDataSize) == WAITRESS_RET_OK) { + return PIANO_RET_OK; } - - curl_slist_free_all (headers); - - return ret; + return PIANO_RET_NET_ERROR; } /* get data @@ -100,23 +56,12 @@ PianoReturn_t PianoHttpPost (CURL *ch, const char *url, const char *postData, * @param put received data here, memory is allocated by this function * @return nothing yet */ -PianoReturn_t PianoHttpGet (CURL *ch, const char *url, char **retData) { - struct PianoHttpBuffer curlRet = {0, NULL}; - PianoReturn_t ret; - - curl_easy_setopt (ch, CURLOPT_URL, url); - curl_easy_setopt (ch, CURLOPT_HTTPGET, 1L); - curl_easy_setopt (ch, CURLOPT_HTTPHEADER, NULL); - curl_easy_setopt (ch, CURLOPT_WRITEFUNCTION, PianoCurlRetToVar); - curl_easy_setopt (ch, CURLOPT_WRITEDATA, (void *) &curlRet); +PianoReturn_t PianoHttpGet (WaitressHandle_t *waith, char *retData, + size_t retDataSize) { + waith->method = WAITRESS_METHOD_GET; - if (curl_easy_perform (ch) == CURLE_OK) { - ret = PIANO_RET_OK; - *retData = curlRet.buf; - } else { - ret = PIANO_RET_NET_ERROR; - *retData = NULL; + if (WaitressFetchBuf (waith, retData, retDataSize) == WAITRESS_RET_OK) { + return PIANO_RET_OK; } - - return ret; + return PIANO_RET_NET_ERROR; } |