-
Notifications
You must be signed in to change notification settings - Fork 1
/
ask_question.c
144 lines (118 loc) · 4.3 KB
/
ask_question.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* sirius is a unix command like sirius client.
* Copyright (C) 2015 https://github.com/d4ndo/sirius-client
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <curl/curl.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include "ask_question.h"
#include "./pcrs/pcrs.h"
#include "globaldefs.h"
int ask_question(const char *url, const char *question, char **answer) {
CURL *curl_handle;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
/* Prepare GET request
* Must be in this form:
* mySiriusHost.com?query=What%20is%20the%20speed%20of%20light
*/
char *modified_url = NULL;
modified_url = prepare_url(url, question);
curl_easy_setopt(curl_handle, CURLOPT_URL, modified_url);
free(modified_url);
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl_handle);
if(res == CURLE_GOT_NOTHING) {
chunk.memory = realloc(chunk.memory, 2 * sizeof(char));
chunk.memory = strndup("\0", 2);
chunk.size = 0;
}
if(res != CURLE_OK && res != CURLE_GOT_NOTHING) {
fprintf(stderr, "curl error num %d\n", res);
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
return -1;
}
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
*answer = chunk.memory;
return chunk.size;
}
static char *prepare_url(const char *url, const char *question)
{
int err = 0;
size_t length;
char *request_question = NULL;
char *myquestion = strndup(question, MAX_DATA_SIZE);
/* remove redundant whitespace */
pcrs_job *job0 = NULL;
char pattern0[] = "s/\\s+/ /g";
if (NULL == (job0 = pcrs_compile_command(pattern0, &err)))
{
fprintf(stderr, "%s Compile error: %s (%d).\n", pattern0, pcrs_strerror(err), err);
exit(EXIT_FAILURE);
}
if (0 > (err = pcrs_execute(job0, myquestion, strlen(myquestion), &request_question, &length)))
{
fprintf(stderr, "Exec error: %s (%d)\n", pcrs_strerror(err), err);
exit(EXIT_FAILURE);
}
free(job0);
/* replace whitespace by %20 */
pcrs_job *job = NULL;
char pattern[] = "s/\\s/%20/g";
if (NULL == (job = pcrs_compile_command(pattern, &err)))
{
fprintf(stderr, "%s Compile error: %s (%d).\n", pattern, pcrs_strerror(err), err);
exit(EXIT_FAILURE);
}
if (0 > (err = pcrs_execute(job, myquestion, strlen(myquestion), &request_question, &length)))
{
fprintf(stderr, "Exec error: %s (%d)\n", pcrs_strerror(err), err);
exit(EXIT_FAILURE);
}
free(job);
/* build new GET Request string and return it */
char *buffer = NULL;
if (0 > asprintf(&buffer, "%s%s%s", url, QUERY, request_question))
{
free(request_question);
fprintf(stderr, "Allocation error @ prepar_url.\n");
exit(EXIT_FAILURE);
}
free(request_question);
return buffer;
}
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}