void print_json_aux(json_t *element, int indent) { switch json_typeof(element) { case JSON_OBJECT: print_json_object(element, indent); break; case JSON_ARRAY: print_json_array(element, indent); break; case JSON_STRING: print_json_string(element, indent); break; case JSON_INTEGER: print_json_integer(element, indent); break; case JSON_REAL: print_json_real(element, indent);; break; case JSON_TRUE: print_json_true(element, indent);; break; case JSON_FALSE: print_json_false(element, indent);; break; case JSON_NULL: print_json_null(element, indent);; break; default: fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element)); } }
void call_producer_json(fdb_file_handle *db, fdb_kvs_handle *kvs, json_t *json) { json_t *json_errors = new_json_errors(); const char *command = json_string_value( get_json_string_required(json_errors, json, "command")); if(command) { if(!strcmp(command, "set")) call_json_set(db, kvs, json_errors, json); else if(!strcmp(command, "delete")) call_json_delete(db, kvs, json_errors, json); else add_custom_json_error(json_errors, "key", "command", "error", "NOT_A_PRODUCER_COMMAND"); } if(json_errors_p(json_errors)) print_json_object(stdout, json_errors); json_decref(json_errors); }
int mlab_ns(char *service, char *mlabns_server, struct sockaddr_in *service_ip) { struct addrinfo *addrs = NULL, *addrs_iter = NULL, addrs_hints; int gai_error = 0; struct sockaddr *server_addr = NULL; int no_dns_results = 1; char *get_linebreak = "\r\n"; char *get = NULL; int get_len = 0; int success = 1; int http_socket = -1; char end_of_header[4] = {'\r', '\n', '\r', '\n',}; int end_of_header_ctr = 0, end_of_header_len = 4; char response[513] = {'\0',}; int response_len_ctr = 0, response_len_max = 42; //512; char b; int in_header = 1; #ifdef DEBUG char buff[512] = {'\0', }; #endif if (!mlabns_server) { mlabns_server = "mlab-ns.appspot.com"; } /* * do server resolution. */ memset(&addrs_hints, 0, sizeof(addrs_hints)); addrs_hints.ai_family = AF_INET; addrs_hints.ai_socktype = SOCK_STREAM; addrs_hints.ai_flags = AI_NUMERICSERV; if ((gai_error = getaddrinfo(mlabns_server, "80", &addrs_hints, &addrs))) { printf("getaddrinfo error: %s\n", gai_strerror(gai_error)); return -1; } server_addr = (struct sockaddr*)malloc(sizeof(struct sockaddr)); addrs_iter = addrs; while (addrs_iter) { memcpy(server_addr, addrs_iter->ai_addr, sizeof(struct sockaddr)); addrs_iter = addrs_iter->ai_next; no_dns_results = 0; #ifdef DEBUG printf("ip: %s\n", inet_ntop(AF_INET, &((struct sockaddr_in*)server_addr)->sin_addr, buff, 512) ); #endif } freeaddrinfo(addrs); if (no_dns_results) { return -2; } get_len = strlen("GET ") + strlen("/") + strlen(service) + strlen(" HTTP/1.1") + strlen(get_linebreak) + strlen("Host: ") + strlen(mlabns_server) + strlen(get_linebreak) + strlen("Connection: close") + strlen(get_linebreak) + strlen(get_linebreak); get = (char*)malloc(get_len + 1); memset(get, 0, get_len+1); strcpy(get, "GET /"); strcat(get, service); strcat(get, " HTTP/1.1"); strcat(get, get_linebreak); strcat(get, "Host: "); strcat(get, mlabns_server); strcat(get, get_linebreak); strcat(get, "Connection: close"); strcat(get, get_linebreak); strcat(get, get_linebreak); printf("get: %s\n", get); if ((http_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { success = -3; goto error; } if (connect(http_socket, server_addr, sizeof(struct sockaddr_in))) { success = -4; goto error; } write(http_socket, get, get_len); while (read(http_socket, &b, 1)) { if (in_header) { if (end_of_header[end_of_header_ctr++] != b) { end_of_header_ctr = 0; } if (end_of_header_ctr>=end_of_header_len) { in_header = 0; } } else { if (response_len_ctr>=response_len_max) break; response[response_len_ctr] = b; response_len_ctr++; } } #ifdef DEBUG printf("response: -%s-\n", response); #endif json_parse_success = 1; input_string = response; input_len = strlen(input_string); yyparse(); if (!json_parse_success) { printf("failed to parse.\n"); goto error; } #ifdef DEBUG if (json_parse_success) print_json_object(root); #endif error: close(http_socket); free(get); return success; }