|
| 1 | +#include "librequests.h" |
| 2 | + |
| 3 | +#include <curl/curl.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include "hashtable.h" |
| 8 | +#include "ast.h" |
| 9 | +#include "eval.h" |
| 10 | + |
| 11 | +typedef struct { |
| 12 | + char *data; |
| 13 | + size_t size; |
| 14 | +} ResponseBuffer; |
| 15 | + |
| 16 | +static size_t curlWriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { |
| 17 | + size_t totalSize = size * nmemb; |
| 18 | + ResponseBuffer *mem = (ResponseBuffer *)userp; |
| 19 | + |
| 20 | + char *ptr = realloc(mem->data, mem->size + totalSize + 1); |
| 21 | + if (ptr == NULL) return 0; // out of memory |
| 22 | + |
| 23 | + mem->data = ptr; |
| 24 | + memcpy(&(mem->data[mem->size]), contents, totalSize); |
| 25 | + mem->size += totalSize; |
| 26 | + mem->data[mem->size] = 0; |
| 27 | + |
| 28 | + return totalSize; |
| 29 | +} |
| 30 | + |
| 31 | +static CURLcode perform_http_request(const char *url, const char *method, int headers_len, |
| 32 | + struct curl_slist *headers, const char *post_data, |
| 33 | + char **out_response) { |
| 34 | + CURL *curl = curl_easy_init(); |
| 35 | + if (!curl) return CURLE_FAILED_INIT; |
| 36 | + |
| 37 | + ResponseBuffer chunk = {.data = malloc(1012), .size = 0}; |
| 38 | + if (!chunk.data) return CURLE_OUT_OF_MEMORY; |
| 39 | + |
| 40 | + curl_easy_setopt(curl, CURLOPT_URL, url); |
| 41 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback); |
| 42 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &chunk); |
| 43 | + if (headers_len > 0) { |
| 44 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 45 | + } |
| 46 | + |
| 47 | + if (strcmp(method, "POST") == 0) { |
| 48 | + curl_easy_setopt(curl, CURLOPT_POST, 1L); |
| 49 | + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data ? post_data : ""); |
| 50 | + } |
| 51 | + |
| 52 | + CURLcode res = curl_easy_perform(curl); |
| 53 | + curl_easy_cleanup(curl); |
| 54 | + |
| 55 | + if (res != CURLE_OK) { |
| 56 | + free(chunk.data); |
| 57 | + return res; |
| 58 | + } |
| 59 | + |
| 60 | + *out_response = chunk.data; |
| 61 | + return CURLE_OK; |
| 62 | +} |
| 63 | + |
| 64 | +static struct curl_slist *build_headers_from_dict(dictionary_t *dict, int *len, |
| 65 | + EXPRESSION_PARAMS()) { |
| 66 | + struct curl_slist *header_list = NULL; |
| 67 | + keyValList_t *kv = dict->keyVals; |
| 68 | + int count = 0; |
| 69 | + |
| 70 | + while (kv) { |
| 71 | + if (kv->key->type == EXPR_TYPE_TEXT && kv->val->type == EXPR_TYPE_TEXT) { |
| 72 | + char header_buf[1024]; |
| 73 | + char *key = NULL; |
| 74 | + char *val = NULL; |
| 75 | + stackval_t sv; |
| 76 | + void *sp = PROVIDE_CONTEXT()->sp; |
| 77 | + size_t *sc = PROVIDE_CONTEXT()->sc; |
| 78 | + int *interactive = PROVIDE_CONTEXT()->interactive; |
| 79 | + |
| 80 | + /* Evaluate the key expression */ |
| 81 | + evaluate_expression(kv->key, EXPRESSION_ARGS()); |
| 82 | + POP_VAL(&sv, sp, sc); |
| 83 | + |
| 84 | + if (sv.type != TEXT) { |
| 85 | + fprintf(stderr, "error: expression for headers must be a string, was: %d\n", sv.type); |
| 86 | + if (!*interactive) { |
| 87 | + exit(1); |
| 88 | + } |
| 89 | + return NULL; |
| 90 | + } |
| 91 | + key = sv.t; |
| 92 | + /* Evaluate the value expression */ |
| 93 | + evaluate_expression(kv->val, EXPRESSION_ARGS()); |
| 94 | + POP_VAL(&sv, sp, sc); |
| 95 | + |
| 96 | + if (sv.type != TEXT) { |
| 97 | + fprintf(stderr, "error: expression for headers must be a string, was: %d\n", sv.type); |
| 98 | + if (!*interactive) { |
| 99 | + exit(1); |
| 100 | + } |
| 101 | + return NULL; |
| 102 | + } |
| 103 | + val = sv.t; |
| 104 | + |
| 105 | + snprintf(header_buf, sizeof(header_buf), "%s: %s", key, val); |
| 106 | + header_list = curl_slist_append(header_list, header_buf); |
| 107 | + count += 1; |
| 108 | + } |
| 109 | + kv = kv->next; |
| 110 | + } |
| 111 | + *len = count; |
| 112 | + return header_list; |
| 113 | +} |
| 114 | + |
| 115 | +static int perform_request(const char *method, EXPRESSION_PARAMS()) { |
| 116 | + stackval_t stv_url, stv_headers, stv_body; |
| 117 | + char *url = NULL; |
| 118 | + dictionary_t *headers_dict = NULL; |
| 119 | + char *post_body = NULL; |
| 120 | + void *sp = PROVIDE_CONTEXT()->sp; |
| 121 | + size_t *sc = PROVIDE_CONTEXT()->sc; |
| 122 | + void *hp = PROVIDE_CONTEXT()->hp; |
| 123 | + heapval_t *hpv = NULL; |
| 124 | + int dummy; |
| 125 | + int headers_len = 0; |
| 126 | + |
| 127 | + POP_VAL(&stv_url, sp, sc); |
| 128 | + POP_VAL(&stv_headers, sp, sc); |
| 129 | + |
| 130 | + if (strcmp(method, "POST") == 0) { |
| 131 | + POP_VAL(&stv_body, sp, sc); |
| 132 | + if (stv_body.type != TEXT) { |
| 133 | + fprintf(stderr, "error: POST body must be a string.\n"); |
| 134 | + return 1; |
| 135 | + } |
| 136 | + post_body = stv_body.t; |
| 137 | + } |
| 138 | + |
| 139 | + if (stv_url.type != TEXT || stv_headers.type != DICTTYPE) { |
| 140 | + fprintf(stderr, "error: '%s' request expects a string (url) and a dictionary (headers).\n", |
| 141 | + method); |
| 142 | + return 1; |
| 143 | + } |
| 144 | + |
| 145 | + url = stv_url.t; |
| 146 | + headers_dict = stv_headers.dict; |
| 147 | + |
| 148 | + struct curl_slist *curl_headers = |
| 149 | + build_headers_from_dict(headers_dict, &headers_len, EXPRESSION_ARGS()); |
| 150 | + |
| 151 | + char *response_body = malloc(1024); |
| 152 | + CURLcode res = |
| 153 | + perform_http_request(url, method, headers_len, curl_headers, post_body, &response_body); |
| 154 | + curl_slist_free_all(curl_headers); |
| 155 | + |
| 156 | + if (res != CURLE_OK) { |
| 157 | + fprintf(stderr, "error: %s HTTP %s request failed: %s\n", url, method, |
| 158 | + curl_easy_strerror(res)); |
| 159 | + PUSH_INT(-1, sp, sc); |
| 160 | + return 0; |
| 161 | + } |
| 162 | + |
| 163 | + stackval_t stv; |
| 164 | + stv.type = TEXT; |
| 165 | + stv.t = response_body; |
| 166 | + |
| 167 | + ALLOC_HEAP(&stv, hp, &hpv, &dummy); |
| 168 | + PUSH_STRING(response_body, sp, sc); |
| 169 | + return 0; |
| 170 | +} |
| 171 | + |
| 172 | +int ric_requests_get(LIBRARY_PARAMS()) { return perform_request("GET", EXPRESSION_ARGS()); } |
| 173 | +int ric_requests_post(LIBRARY_PARAMS()) { return perform_request("POST", EXPRESSION_ARGS()); } |
0 commit comments