Check status code for GS api requests

This commit is contained in:
Iwan Timmer
2017-08-06 22:06:55 +02:00
parent eb2bdad535
commit 1b8ac983e5
5 changed files with 72 additions and 9 deletions

View File

@@ -23,6 +23,8 @@
#include <expat.h>
#include <string.h>
#define STATUS_OK 200
static XML_Parser parser;
struct xml_query {
@@ -110,6 +112,23 @@ static void XMLCALL _xml_end_mode_element(void *userData, const char *name) {
}
}
static void XMLCALL _xml_start_status_element(void *userData, const char *name, const char **atts) {
if (strcmp("root", name) == 0) {
int* status = (int*) userData;
for (int i = 0; atts[i]; i += 2) {
if (strcmp("status_code", atts[i]) == 0)
*status = atoi(atts[i + 1]);
else if (*status != STATUS_OK && strcmp("status_message", atts[i]) == 0) {
gs_error = malloc(strlen(atts[i + 1]));
if (gs_error)
strcpy((char*) gs_error, atts[i + 1]);
}
}
}
}
static void XMLCALL _xml_end_status_element(void *userData, const char *name) { }
static void XMLCALL _xml_write_data(void *userData, const XML_Char *s, int len) {
struct xml_query *search = (struct xml_query*) userData;
if (search->start > 0) {
@@ -193,3 +212,19 @@ int xml_modelist(char* data, size_t len, PDISPLAY_MODE *mode_list) {
return GS_OK;
}
int xml_status(char* data, size_t len) {
int status = 0;
XML_Parser parser = XML_ParserCreate("UTF-8");
XML_SetUserData(parser, &status);
XML_SetElementHandler(parser, _xml_start_status_element, _xml_end_status_element);
if (!XML_Parse(parser, data, len, 1)) {
int code = XML_GetErrorCode(parser);
gs_error = XML_ErrorString(code);
XML_ParserFree(parser);
return GS_INVALID;
}
XML_ParserFree(parser);
return status == STATUS_OK ? GS_OK : GS_ERROR;
}