-/* ------------------------------------------------\r
-Copyright 2014 AT&T Intellectual Property\r
- Licensed under the Apache License, Version 2.0 (the "License");\r
- you may not use this file except in compliance with the License.\r
- You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
- ------------------------------------------- */\r
-\r
-#include <arpa/inet.h>\r
-#include <netinet/in.h>\r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <sys/types.h>\r
-#include <sys/socket.h>\r
-#include <sys/wait.h>\r
-#include <netdb.h>\r
-#include <unistd.h>\r
-\r
-#include "simple_http.h"\r
-\r
-#define MAX_HTTP_REQUEST 64 * 1024\r
-\r
-\r
-// perform HTTP GET and retrieve the json response\r
-int http_get_request(endpoint addr, gs_csp_t url, gs_uint32_t* http_code, gs_sp_t json_response) {\r
-\r
- int sockfd;\r
- struct sockaddr_in servaddr;\r
- char request_buf[MAX_HTTP_REQUEST];\r
- char response_buf[MAX_HTTP_REQUEST];\r
- char temp_buf[MAX_HTTP_REQUEST];\r
- char content_type[MAX_HTTP_REQUEST];\r
- int n;\r
-\r
- char ipstr[16];\r
- inet_ntop(AF_INET, &addr.ip, ipstr, INET_ADDRSTRLEN);\r
-\r
- sockfd = socket(AF_INET, SOCK_STREAM, 0);\r
- if (!sockfd) {\r
- fprintf(stderr, "Unable to create socket for HTTP connection\n");\r
- return -1;\r
- }\r
-\r
- bzero(&servaddr, sizeof(servaddr));\r
- servaddr.sin_family = AF_INET;\r
- servaddr.sin_port = addr.port;\r
- servaddr.sin_addr.s_addr = addr.ip;\r
-\r
- if (connect(sockfd, (sockaddr*)&servaddr, sizeof(servaddr))) {\r
- fprintf(stderr, "Unable to connect to HTTP server\n");\r
- return -1;\r
- }\r
-\r
- // construct HTTP request\r
- sprintf(request_buf, "GET %s HTTP/1.0\r\n"\r
- "Host: %s\r\n"\r
- "Accept: application/json\r\n"\r
- "Connection: close\r\n\r\n",\r
- url, ipstr);\r
-\r
- write(sockfd, request_buf, strlen(request_buf));\r
-\r
- // retrive HTTP response\r
- char* write_pos = response_buf;\r
- while ((n = read(sockfd, write_pos, MAX_HTTP_REQUEST)) > 0)\r
- write_pos += n;\r
- *write_pos = '\0';\r
- close(sockfd);\r
-\r
- // split HTTP response into header and the main body\r
- char* header_end = strstr(response_buf, "\r\n\r\n");\r
- *header_end = 0;\r
- strcpy(json_response, header_end + 4);\r
-\r
- // parse HTTP header\r
- char* header_line = strtok(response_buf, "\r\n");\r
- int ver1, ver2;\r
-\r
- // extract http response code\r
- sscanf (header_line, "HTTP/%d.%d %d %s", &ver1, &ver2, http_code, temp_buf);\r
-\r
- // extract content-type\r
- *content_type = 0;\r
- while ((header_line = strtok(NULL, "\r\n"))) {\r
- if (!strncmp(header_line, "Content-Type: ", strlen("Content-Type: "))) {\r
- strcpy(content_type, header_line + strlen("Content-Type: "));\r
- if (strcmp(content_type, "application/json")) {\r
- fprintf(stderr, "Invalid Content-Type %s, application/json expected\n", content_type);\r
- return -1;\r
- }\r
- break; // we only care about Content-Type headers\r
- }\r
- }\r
-\r
- if ((*http_code == 200) && (*content_type == 0)) {\r
- fprintf(stderr, "Missing Content-Type in server response, application/json expected\n");\r
- return -1;\r
- }\r
-\r
-\r
- return 0;\r
-}\r
-\r
-// perform HTTP POST request ignoring the response\r
-int http_post_request(endpoint addr, gs_csp_t url, gs_sp_t json_request, gs_uint32_t* http_code) {\r
- int sockfd;\r
- struct sockaddr_in servaddr;\r
- char request_buf[MAX_HTTP_REQUEST];\r
- char response_buf[MAX_HTTP_REQUEST];\r
- char temp_buf[MAX_HTTP_REQUEST];\r
- int n;\r
-\r
- char ipstr[16];\r
- inet_ntop(AF_INET, &addr.ip, ipstr, INET_ADDRSTRLEN);\r
-\r
- sockfd = socket(AF_INET, SOCK_STREAM, 0);\r
- if (!sockfd) {\r
- fprintf(stderr, "Unable to create socket for HTTP connection\n");\r
- return -1;\r
- }\r
-\r
- bzero(&servaddr, sizeof(servaddr));\r
- servaddr.sin_family = AF_INET;\r
- servaddr.sin_port = addr.port;\r
- servaddr.sin_addr.s_addr = addr.ip;\r
-\r
- if (connect(sockfd, (sockaddr*)&servaddr, sizeof(servaddr))) {\r
- fprintf(stderr, "Unable to connect to HTTP server\n");\r
- return -1;\r
- }\r
-\r
- // construct HTTP request\r
- sprintf(request_buf, "POST %s HTTP/1.0\r\n"\r
- "Host: %s\r\n"\r
- "Content-Type: application/json\r\n"\r
- "Content-Length: %lu\r\n"\r
- "Connection: close\r\n\r\n"\r
- "%s",\r
- url, ipstr, strlen(json_request), json_request);\r
-\r
- write(sockfd, request_buf, strlen(request_buf));\r
-\r
- // retrive HTTP response\r
- char* write_pos = response_buf;\r
- while ((n = read(sockfd, write_pos, MAX_HTTP_REQUEST)) > 0)\r
- write_pos += n;\r
- *write_pos = '\0';\r
- close(sockfd);\r
-\r
- // parse HTTP header\r
- char* header_line = strtok(response_buf, "\r\n");\r
- int ver1, ver2;\r
-\r
- // extract http response code\r
- sscanf (header_line, "HTTP/%d.%d %d %s", &ver1, &ver2, http_code, temp_buf);\r
-\r
- return 0;\r
-}\r
-\r
-\r
-// perform HTTP POST request ignoring the response\r
-int http_post_request_hdr(endpoint addr, gs_csp_t url, gs_sp_t json_request, gs_uint32_t* http_code, gs_sp_t extra_headers) {\r
- int sockfd;\r
- struct sockaddr_in servaddr;\r
- char request_buf[MAX_HTTP_REQUEST];\r
- char response_buf[MAX_HTTP_REQUEST];\r
- char temp_buf[MAX_HTTP_REQUEST];\r
- int n;\r
-\r
- char ipstr[16];\r
- inet_ntop(AF_INET, &addr.ip, ipstr, INET_ADDRSTRLEN);\r
-\r
- sockfd = socket(AF_INET, SOCK_STREAM, 0);\r
- if (!sockfd) {\r
- fprintf(stderr, "Unable to create socket for HTTP connection\n");\r
- return -1;\r
- }\r
-\r
- bzero(&servaddr, sizeof(servaddr));\r
- servaddr.sin_family = AF_INET;\r
- servaddr.sin_port = addr.port;\r
- servaddr.sin_addr.s_addr = addr.ip;\r
-\r
- if (connect(sockfd, (sockaddr*)&servaddr, sizeof(servaddr))) {\r
- fprintf(stderr, "Unable to connect to HTTP server\n");\r
- return -1;\r
- }\r
-\r
- // construct HTTP request\r
- if(strlen(extra_headers)==0){\r
- sprintf(request_buf, "POST %s HTTP/1.0\r\n"\r
- "Host: %s\r\n"\r
- "Content-Type: application/json\r\n"\r
- "Content-Length: %lu\r\n"\r
- "\r\n"\r
- "%s",\r
- url, ipstr, strlen(json_request), json_request);\r
- }else{\r
- sprintf(request_buf, "POST %s HTTP/1.0\r\n"\r
- "Host: %s\r\n"\r
- "Content-Type: application/json\r\n"\r
- "Content-Length: %lu\r\n"\r
- "%s\r\n\r\n"\r
- "%s",\r
- url, ipstr, strlen(json_request), extra_headers, json_request);\r
- }\r
-\r
- write(sockfd, request_buf, strlen(request_buf));\r
-\r
- // retrive HTTP response\r
- char* write_pos = response_buf;\r
- while ((n = read(sockfd, write_pos, MAX_HTTP_REQUEST)) > 0)\r
- write_pos += n;\r
- *write_pos = '\0';\r
- close(sockfd);\r
- if(write_pos==response_buf){\r
- fprintf(stderr,"Empty response from HTTP server.\n");\r
- return -1;\r
- }\r
-\r
- // parse HTTP header\r
- char* header_line = strtok(response_buf, "\r\n");\r
- if(header_line==NULL){\r
- fprintf(stderr,"unparseable response from HTTP server.\n");\r
- return -1;\r
- }\r
- int ver1, ver2;\r
-\r
- // extract http response code\r
- sscanf (header_line, "HTTP/%d.%d %d %s", &ver1, &ver2, http_code, temp_buf);\r
-\r
- return 0;\r
-}\r
+/* ------------------------------------------------
+Copyright 2014 AT&T Intellectual Property
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ ------------------------------------------- */
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <netdb.h>
+#include <unistd.h>
+
+#include "simple_http.h"
+
+#define MAX_HTTP_REQUEST 64 * 1024
+
+
+// perform HTTP GET and retrieve the json response
+int http_get_request(endpoint addr, gs_csp_t url, gs_uint32_t* http_code, gs_sp_t json_response) {
+
+ int sockfd;
+ struct sockaddr_in servaddr;
+ char request_buf[MAX_HTTP_REQUEST];
+ char response_buf[MAX_HTTP_REQUEST];
+ char temp_buf[MAX_HTTP_REQUEST];
+ char content_type[MAX_HTTP_REQUEST];
+ int n;
+
+ char ipstr[16];
+ inet_ntop(AF_INET, &addr.ip, ipstr, INET_ADDRSTRLEN);
+
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (!sockfd) {
+ fprintf(stderr, "Unable to create socket for HTTP connection\n");
+ return -1;
+ }
+
+ bzero(&servaddr, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = addr.port;
+ servaddr.sin_addr.s_addr = addr.ip;
+
+ if (connect(sockfd, (sockaddr*)&servaddr, sizeof(servaddr))) {
+ fprintf(stderr, "Unable to connect to HTTP server\n");
+ return -1;
+ }
+
+ // construct HTTP request
+ sprintf(request_buf, "GET %s HTTP/1.0\r\n"
+ "Host: %s\r\n"
+ "Accept: application/json\r\n"
+ "Connection: close\r\n\r\n",
+ url, ipstr);
+
+ write(sockfd, request_buf, strlen(request_buf));
+
+ // retrive HTTP response
+ char* write_pos = response_buf;
+ while ((n = read(sockfd, write_pos, MAX_HTTP_REQUEST)) > 0)
+ write_pos += n;
+ *write_pos = '\0';
+ close(sockfd);
+
+ // split HTTP response into header and the main body
+ char* header_end = strstr(response_buf, "\r\n\r\n");
+ *header_end = 0;
+ strcpy(json_response, header_end + 4);
+
+ // parse HTTP header
+ char* header_line = strtok(response_buf, "\r\n");
+ int ver1, ver2;
+
+ // extract http response code
+ sscanf (header_line, "HTTP/%d.%d %d %s", &ver1, &ver2, http_code, temp_buf);
+
+ // extract content-type
+ *content_type = 0;
+ while ((header_line = strtok(NULL, "\r\n"))) {
+ if (!strncmp(header_line, "Content-Type: ", strlen("Content-Type: "))) {
+ strcpy(content_type, header_line + strlen("Content-Type: "));
+ if (strcmp(content_type, "application/json")) {
+ fprintf(stderr, "Invalid Content-Type %s, application/json expected\n", content_type);
+ return -1;
+ }
+ break; // we only care about Content-Type headers
+ }
+ }
+
+ if ((*http_code == 200) && (*content_type == 0)) {
+ fprintf(stderr, "Missing Content-Type in server response, application/json expected\n");
+ return -1;
+ }
+
+
+ return 0;
+}
+
+// perform HTTP POST request ignoring the response
+int http_post_request(endpoint addr, gs_csp_t url, gs_sp_t json_request, gs_uint32_t* http_code) {
+ int sockfd;
+ struct sockaddr_in servaddr;
+ char request_buf[MAX_HTTP_REQUEST];
+ char response_buf[MAX_HTTP_REQUEST];
+ char temp_buf[MAX_HTTP_REQUEST];
+ int n;
+
+ char ipstr[16];
+ inet_ntop(AF_INET, &addr.ip, ipstr, INET_ADDRSTRLEN);
+
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (!sockfd) {
+ fprintf(stderr, "Unable to create socket for HTTP connection\n");
+ return -1;
+ }
+
+ bzero(&servaddr, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = addr.port;
+ servaddr.sin_addr.s_addr = addr.ip;
+
+ if (connect(sockfd, (sockaddr*)&servaddr, sizeof(servaddr))) {
+ fprintf(stderr, "Unable to connect to HTTP server\n");
+ return -1;
+ }
+
+ // construct HTTP request
+ sprintf(request_buf, "POST %s HTTP/1.0\r\n"
+ "Host: %s\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: %lu\r\n"
+ "Connection: close\r\n\r\n"
+ "%s",
+ url, ipstr, strlen(json_request), json_request);
+
+ write(sockfd, request_buf, strlen(request_buf));
+
+ // retrive HTTP response
+ char* write_pos = response_buf;
+ while ((n = read(sockfd, write_pos, MAX_HTTP_REQUEST)) > 0)
+ write_pos += n;
+ *write_pos = '\0';
+ close(sockfd);
+
+ // parse HTTP header
+ char* header_line = strtok(response_buf, "\r\n");
+ int ver1, ver2;
+
+ // extract http response code
+ sscanf (header_line, "HTTP/%d.%d %d %s", &ver1, &ver2, http_code, temp_buf);
+
+ return 0;
+}
+
+
+// perform HTTP POST request ignoring the response
+int http_post_request_hdr(endpoint addr, gs_csp_t url, gs_sp_t json_request, gs_uint32_t* http_code, gs_sp_t extra_headers) {
+ int sockfd;
+ struct sockaddr_in servaddr;
+ char request_buf[MAX_HTTP_REQUEST];
+ char response_buf[MAX_HTTP_REQUEST];
+ char temp_buf[MAX_HTTP_REQUEST];
+ int n;
+
+ char ipstr[16];
+ inet_ntop(AF_INET, &addr.ip, ipstr, INET_ADDRSTRLEN);
+
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (!sockfd) {
+ fprintf(stderr, "Unable to create socket for HTTP connection\n");
+ return -1;
+ }
+
+ bzero(&servaddr, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = addr.port;
+ servaddr.sin_addr.s_addr = addr.ip;
+
+ if (connect(sockfd, (sockaddr*)&servaddr, sizeof(servaddr))) {
+ fprintf(stderr, "Unable to connect to HTTP server\n");
+ return -1;
+ }
+
+ // construct HTTP request
+ if(strlen(extra_headers)==0){
+ sprintf(request_buf, "POST %s HTTP/1.0\r\n"
+ "Host: %s\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: %lu\r\n"
+ "\r\n"
+ "%s",
+ url, ipstr, strlen(json_request), json_request);
+ }else{
+ sprintf(request_buf, "POST %s HTTP/1.0\r\n"
+ "Host: %s\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: %lu\r\n"
+ "%s\r\n\r\n"
+ "%s",
+ url, ipstr, strlen(json_request), extra_headers, json_request);
+ }
+
+ write(sockfd, request_buf, strlen(request_buf));
+
+ // retrive HTTP response
+ char* write_pos = response_buf;
+ while ((n = read(sockfd, write_pos, MAX_HTTP_REQUEST)) > 0)
+ write_pos += n;
+ *write_pos = '\0';
+ close(sockfd);
+ if(write_pos==response_buf){
+ fprintf(stderr,"Empty response from HTTP server.\n");
+ return -1;
+ }
+
+ // parse HTTP header
+ char* header_line = strtok(response_buf, "\r\n");
+ if(header_line==NULL){
+ fprintf(stderr,"unparseable response from HTTP server.\n");
+ return -1;
+ }
+ int ver1, ver2;
+
+ // extract http response code
+ sscanf (header_line, "HTTP/%d.%d %d %s", &ver1, &ver2, http_code, temp_buf);
+
+ return 0;
+}