int parse_request(char* buff, int sock_fd) { //TODO: fix the order of the read_from_client, parse_request.. functions char* buff_copy = calloc(1, SIZE); strncpy(buff_copy, buff, SIZE); int num_lines = 0; char* newline = strtok(buff_copy, "\n"); while(newline != NULL) { num_lines +=1; newline = strtok(NULL, "\n"); } char** lines = calloc(num_lines, sizeof(char *)); int i = 0; char* substr = strtok(buff, "\n"); lines[i] = substr; while(substr != NULL){ i++; substr = strtok(NULL, "\n"); lines[i] = substr; } //since strtok modifies its argument, I'm creating a copy of lines to figure //out the type(GET, POST, HEAD) of request char** str = calloc(num_lines, sizeof(char *)); for (int x = 0; x < i; x++) { str[x] = calloc(SIZE, sizeof(char)); strncpy(str[x], lines[x], SIZE * sizeof(char)); } //determine type of request from the first word of the message //should probably do it in a more organized fashion char* tok = strtok(str[0], " "); if(strcmp(tok, "GET") == 0) { if(parse_get_request(sock_fd, lines, num_lines)) { return 1; } else { return 0; } } else if (strcmp(tok, "HEAD") == 0) { parse_head_request(sock_fd, lines, num_lines); } else { perror("invalid request lol"); return 0; } printf("finished parsing\n"); return 1; }
void wdmp_parse_generic_request(char * payload, PAYLOAD_TYPE payload_type, req_struct **reqObj) { cJSON *request = NULL; char *out = NULL, *command = NULL; if (!payload || !reqObj) { WdmpPrint("wdmp_parse_generic_request - invalid param!\n"); return; } request = cJSON_Parse(payload); if (request != NULL) { if(cJSON_GetObjectItem(request, "command") != NULL) { command = cJSON_GetObjectItem(request, "command")->valuestring; WdmpPrint("command %s\n", (command == NULL) ? "NULL" : command); } if (command != NULL) { out = cJSON_PrintUnformatted(request); //allocate structure according to payload type if (payload_type == WDMP_TR181 || payload_type == WDMP_SNMP) { (*reqObj) = (req_struct *) malloc(sizeof(req_struct)); memset((*reqObj), 0, sizeof(req_struct)); } else { // allocate according to payload type. // - currently no other data types supported WdmpPrint("wdmp_parse_generic_request - invalid payload_type : %d\n", payload_type); cJSON_Delete(request); return; } if ((strcmp(command, "GET") == 0) || (strcmp(command, "GET_ATTRIBUTES") == 0)) { WdmpInfo("Request %s\n", out); parse_get_request(request, reqObj, payload_type); } else if ((strcmp(command, "SET") == 0)) { WdmpInfo("SET Request\n"); parse_set_request(request, reqObj, payload_type); } else if ((strcmp(command, "SET_ATTRIBUTES") == 0)) { WdmpInfo("SET ATTRIBUTES Request\n"); parse_set_attr_request(request, reqObj); } else if (strcmp(command, "TEST_AND_SET") == 0) { WdmpInfo("Test and Set Request\n"); parse_test_and_set_request(request, reqObj); } else if (strcmp(command, "REPLACE_ROWS") == 0) { WdmpInfo("REPLACE_ROWS Request\n"); parse_replace_rows_request(request, reqObj); } else if (strcmp(command, "ADD_ROW") == 0) { WdmpInfo("ADD_ROW Request\n"); parse_add_row_request(request, reqObj); } else if (strcmp(command, "DELETE_ROW") == 0) { WdmpInfo("DELETE_ROW Request: %s\n", out); parse_delete_row_request(request, reqObj); } else { WdmpError("Unknown Command \n"); wdmp_free_req_struct(*reqObj); (*reqObj) = NULL; } if (out != NULL) { free(out); } } cJSON_Delete(request); } else { WdmpInfo("Empty payload\n"); } return; }