forked from moonlight-stream/moonlight-embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
132 lines (105 loc) · 3.29 KB
/
http.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
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight 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.
*
* Moonlight 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 Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "http.h"
#include "errors.h"
#include <stdbool.h>
#include <string.h>
#include <curl/curl.h>
static CURL *curl;
static const char *pCertFile = "./client.pem";
static const char *pKeyFile = "./key.pem";
static bool debug;
static size_t _write_curl(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
PHTTP_DATA mem = (PHTTP_DATA)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL)
return 0;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int http_init(const char* keyDirectory, int logLevel) {
curl = curl_easy_init();
debug = logLevel >= 2;
if (!curl)
return GS_FAILED;
char certificateFilePath[4096];
sprintf(certificateFilePath, "%s/%s", keyDirectory, CERTIFICATE_FILE_NAME);
char keyFilePath[4096];
sprintf(&keyFilePath[0], "%s/%s", keyDirectory, KEY_FILE_NAME);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE,"PEM");
curl_easy_setopt(curl, CURLOPT_SSLCERT, certificateFilePath);
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
curl_easy_setopt(curl, CURLOPT_SSLKEY, keyFilePath);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_curl);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0L);
return GS_OK;
}
int http_request(char* url, PHTTP_DATA data) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
curl_easy_setopt(curl, CURLOPT_URL, url);
if (debug)
printf("Request %s\n", url);
if (data->size > 0) {
free(data->memory);
data->memory = malloc(1);
if(data->memory == NULL)
return GS_OUT_OF_MEMORY;
data->size = 0;
}
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) {
gs_error = curl_easy_strerror(res);
return GS_FAILED;
} else if (data->memory == NULL) {
return GS_OUT_OF_MEMORY;
}
if (debug)
printf("Response:\n%s\n\n", data->memory);
return GS_OK;
}
void http_cleanup() {
curl_easy_cleanup(curl);
}
PHTTP_DATA http_create_data() {
PHTTP_DATA data = malloc(sizeof(HTTP_DATA));
if (data == NULL)
return NULL;
data->memory = malloc(1);
if(data->memory == NULL) {
free(data);
return NULL;
}
data->size = 0;
return data;
}
void http_free_data(PHTTP_DATA data) {
if (data != NULL) {
if (data->memory != NULL)
free(data->memory);
free(data);
}
}