Browse Source

Allow post requests

Christoph Haas 10 months ago
parent
commit
86c489f573
3 changed files with 73 additions and 29 deletions
  1. 1
    0
      gclogger.example.ini
  2. 1
    0
      include/gclogger.h
  3. 71
    29
      src/gclogger.c

+ 1
- 0
gclogger.example.ini View File

@@ -29,6 +29,7 @@ interval=60
29 29
 [custlog]
30 30
 ;id=example_device_id
31 31
 ;url=https://api.mygclog.com/log
32
+;type=GET
32 33
 ;param_id=id
33 34
 ;param_cpm=cpm
34 35
 ;param_temp=temp

+ 1
- 0
include/gclogger.h View File

@@ -37,6 +37,7 @@ typedef struct
37 37
     char* safecast_device;
38 38
     char* netc_id;
39 39
     char* custlog_url;
40
+    char* custlog_type;
40 41
     char* custlog_id;
41 42
     char* custlog_param_id;
42 43
     char* custlog_param_cpm;

+ 71
- 29
src/gclogger.c View File

@@ -30,6 +30,8 @@ static int confighandler(void* config, const char* section, const char* name, co
30 30
         pconfig->dev_interval = atoi(value);
31 31
     } else if (MATCH("custlog", "url")) {
32 32
         pconfig->custlog_url = strdup(value);
33
+    } else if (MATCH("custlog", "type")) {
34
+        pconfig->custlog_type = strdup(value);
33 35
     } else if (MATCH("custlog", "id")) {
34 36
         pconfig->custlog_id = strdup(value);
35 37
     } else if (MATCH("custlog", "param_id")) {
@@ -70,6 +72,7 @@ void init_configuration(configuration* config) {
70 72
 	config->dev_longitude = 0.0;
71 73
 	config->dev_interval = 60;
72 74
 	config->custlog_url = "";
75
+	config->custlog_type = "GET";
73 76
 	config->custlog_id = "";
74 77
 	config->custlog_param_id = "id";
75 78
 	config->custlog_param_cpm = "cpm";
@@ -98,42 +101,76 @@ bool send_custlog(const configuration config, int cpm, float temperature, const
98 101
 	CURL *curl;
99 102
 	CURLcode res;
100 103
 
101
-	curl_global_init(CURL_GLOBAL_DEFAULT);
102
-
103 104
 	curl = curl_easy_init();
104 105
 
105 106
 	if(curl) {
106
-		char *url_buffer;
107
+		char *url_buffer = NULL;
107 108
 		size_t url_size;
108
-		// first get size of final url
109
-		url_size = snprintf(NULL, 0, "%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s", 
110
-			config.custlog_url,
111
-			config.custlog_param_id, config.custlog_id,
112
-			config.custlog_param_cpm, cpm,
113
-			config.custlog_param_temp, temperature,
114
-			config.custlog_param_version, version,
115
-			config.custlog_param_time, mktime(tm),
116
-			config.custlog_param_lng, config.dev_longitude,
117
-			config.custlog_param_lat, config.dev_latitude,
118
-			config.custlog_param_loc, config.dev_location);
119
-
120
-		// now allocate buffer and build url
121
-		url_buffer = (char *)malloc(url_size + 1);
122
-		snprintf(url_buffer, url_size+1,"%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s", 
123
-			config.custlog_url,
124
-			config.custlog_param_id, config.custlog_id,
125
-			config.custlog_param_cpm, cpm,
126
-			config.custlog_param_temp, temperature,
127
-			config.custlog_param_version, version,
128
-			config.custlog_param_time, mktime(tm),
129
-			config.custlog_param_lng, config.dev_longitude,
130
-			config.custlog_param_lat, config.dev_latitude,
131
-			config.custlog_param_loc, config.dev_location);
132
-
133
-		printf("final url: %s\n", url_buffer);
109
+		char *post_buffer = NULL;
110
+		size_t post_size;
111
+
112
+		if(strncmp(config.custlog_type, "GET", 3) == 0) {
113
+			// first get size of final url
114
+			url_size = snprintf(NULL, 0, "%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s", 
115
+				config.custlog_url,
116
+				config.custlog_param_id, config.custlog_id,
117
+				config.custlog_param_cpm, cpm,
118
+				config.custlog_param_temp, temperature,
119
+				config.custlog_param_version, version,
120
+				config.custlog_param_time, mktime(tm),
121
+				config.custlog_param_lng, config.dev_longitude,
122
+				config.custlog_param_lat, config.dev_latitude,
123
+				config.custlog_param_loc, config.dev_location);
124
+
125
+			// now allocate buffer and build url
126
+			url_buffer = (char *)malloc(url_size + 1);
127
+			snprintf(url_buffer, url_size+1,"%s?%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s", 
128
+				config.custlog_url,
129
+				config.custlog_param_id, config.custlog_id,
130
+				config.custlog_param_cpm, cpm,
131
+				config.custlog_param_temp, temperature,
132
+				config.custlog_param_version, version,
133
+				config.custlog_param_time, mktime(tm),
134
+				config.custlog_param_lng, config.dev_longitude,
135
+				config.custlog_param_lat, config.dev_latitude,
136
+				config.custlog_param_loc, config.dev_location);
137
+
138
+			if(config.debug) {
139
+				printf("Final url: %s\n", url_buffer);
140
+			}
141
+		} else {
142
+			url_buffer = strdup(config.custlog_url);
143
+		}
134 144
 
135 145
 		curl_easy_setopt(curl, CURLOPT_URL, url_buffer);
136 146
 
147
+		if(strncmp(config.custlog_type, "POST", 4) == 0) {
148
+			// first get size of final url
149
+			post_size = snprintf(NULL, 0, "%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s",
150
+				config.custlog_param_id, config.custlog_id,
151
+				config.custlog_param_cpm, cpm,
152
+				config.custlog_param_temp, temperature,
153
+				config.custlog_param_version, version,
154
+				config.custlog_param_time, mktime(tm),
155
+				config.custlog_param_lng, config.dev_longitude,
156
+				config.custlog_param_lat, config.dev_latitude,
157
+				config.custlog_param_loc, config.dev_location);
158
+
159
+			// now allocate buffer and build url
160
+			post_buffer = (char *)malloc(post_size + 1);
161
+			snprintf(post_buffer, post_size+1,"%s=%s&%s=%d&%s=%f&%s=%s&%s=%ld&%s=%f&%s=%f&%s=%s",
162
+				config.custlog_param_id, config.custlog_id,
163
+				config.custlog_param_cpm, cpm,
164
+				config.custlog_param_temp, temperature,
165
+				config.custlog_param_version, version,
166
+				config.custlog_param_time, mktime(tm),
167
+				config.custlog_param_lng, config.dev_longitude,
168
+				config.custlog_param_lat, config.dev_latitude,
169
+				config.custlog_param_loc, config.dev_location);
170
+
171
+			curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_buffer);
172
+		}
173
+
137 174
 		#ifdef SKIP_PEER_VERIFICATION
138 175
 		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
139 176
 		#endif
@@ -149,6 +186,8 @@ bool send_custlog(const configuration config, int cpm, float temperature, const
149 186
 			printf("curl_easy_perform() ok!");
150 187
 	 
151 188
 	 	free(url_buffer);
189
+		if(post_buffer != NULL)
190
+	 		free(post_buffer);
152 191
 		curl_easy_cleanup(curl);
153 192
 
154 193
 		return true;
@@ -196,6 +235,9 @@ int main(int argc, char *argv[]) {
196 235
 	signal(SIGQUIT, signal_handler);
197 236
 	signal(SIGHUP, signal_handler);
198 237
 
238
+	// setup curl
239
+	curl_global_init(CURL_GLOBAL_DEFAULT);
240
+
199 241
 	// parse config file
200 242
 	init_configuration(&config);
201 243
 	while ((opt = getopt(argc, argv, "c:d")) != -1) {

Loading…
Cancel
Save