void callback_func(struct oops_log_msg *msg) { strncpy(reason, msg->lines[0], strlen(msg->lines[0]) + 1); pl = parse_payload(msg); }
/** * Parse and return a single request from the YAML URLs file. * * It is assumed that the `parser` has just encountered a * YAML_MAPPING_START_EVENT, and the next events will be two * YAML_SCALAR_EVENTs, corresponding to the method and URL. * * When this function returns, the `parser` will have just * processed a YAML_MAPPING_END_EVENT. * * The return value is a pointer to a request, or NULL if there * was an error; in the event of an error, no assumption should * be made about the state of the parser. */ request* parse_request(yaml_parser_t* parser) { request* req; unsigned long expected_ends = 1; unsigned long i; if (!(req = malloc(sizeof(request)))) goto parse_request_error; req->url = NULL; req->payload_length = 0; req->payload = NULL; req->headers = NULL; req->num_headers = 0; req->curl_headers = NULL; yaml_event_t event; // parse the method if (!yaml_parser_parse(parser, &event)) goto parse_request_error; if (0 == strncmp("get", (const char*)event.data.scalar.value, 3)) { req->method = HTTP_GET; } else if (0 == strncmp("post", (const char*)event.data.scalar.value, 4)) { req->method = HTTP_POST; } else { fprintf(stderr, "Unknown HTTP method '%s'\n", event.data.scalar.value); goto parse_request_error; } yaml_event_delete(&event); // parse the URL if (!yaml_parser_parse(parser, &event)) goto parse_request_error; if (0 >= strlen((const char*)event.data.scalar.value)) { fprintf(stderr, "URL is too short\n"); goto parse_request_error; } if (!(req->url = malloc(sizeof(char) * (strlen((const char*)event.data.scalar.value) + 1)))) goto parse_request_error; strcpy(req->url, (const char*)event.data.scalar.value); yaml_event_delete(&event); // parse the payload or headers while (expected_ends > 0) { // printf(" expected_ends: %lu\n", expected_ends); if (!yaml_parser_parse(parser, &event)) goto parse_request_error; switch (event.type) { case YAML_MAPPING_END_EVENT: // printf(" got MAPPING_END\n"); expected_ends--; break; case YAML_MAPPING_START_EVENT: // printf(" got MAPPING_START\n"); expected_ends++; break; case YAML_SCALAR_EVENT: if (0 == strncmp("payload", (const char*)event.data.scalar.value, 7)) { // printf(" calling parse_payload()\n"); if (parse_payload(parser, &event, req)) goto parse_request_error; } else if (0 == strncmp("headers", (const char*)event.data.scalar.value, 7)) { // make sure "headers:" is followed by a sequence yaml_event_delete(&event); if (!yaml_parser_parse(parser, &event)) goto parse_request_error; if (event.type != YAML_SEQUENCE_START_EVENT) goto parse_request_error; // printf(" calling parse_headers()\n"); if (parse_headers(parser, req)) { goto parse_request_error; } } else { fprintf(stderr, "Unknown request metadata '%s'\n", event.data.scalar.value); goto parse_request_error; } break; default: fprintf(stderr, "unexpected YAML event: %d\n", event.type); goto parse_request_error; } } return req; parse_request_error: // printf("in parse_request_error\n"); if (req != NULL ) { if (req->url != NULL) free(req->url); if (req->payload != NULL) free(req->payload); if (req->headers != NULL) { for (i=0; i<req->num_headers; i++) { free(req->headers[i].name); free(req->headers[i].value); } curl_slist_free_all(req->curl_headers); free(req->headers); } free(req); } return NULL; }