Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 19:57:37 2014 +0000
Revision:
2:2f9019c5a9fc
Parent:
0:65004368569c
ip switch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 #include "axHTTP.h"
AxedaCorp 0:65004368569c 2 #include "axStatusCodes.h"
AxedaCorp 0:65004368569c 3 #include "axConstants.h"
AxedaCorp 0:65004368569c 4 #include "ctype.h"
AxedaCorp 0:65004368569c 5 #include <string.h>
AxedaCorp 0:65004368569c 6 #include <stdio.h>
AxedaCorp 0:65004368569c 7 #include <stdlib.h>
AxedaCorp 0:65004368569c 8 #include "axSettings.h"
AxedaCorp 0:65004368569c 9 #include "axTransport.h"
AxedaCorp 0:65004368569c 10
AxedaCorp 0:65004368569c 11 #define AX_HTTP_OK 200
AxedaCorp 0:65004368569c 12 #define AX_HTTP_SYNTAX_INVALID 400 //The Request syntax is incorrect
AxedaCorp 0:65004368569c 13 #define AX_HTTP_UNAUTHORIZED 401 //You need permission to access that resource. Agent Authentication failure
AxedaCorp 0:65004368569c 14 #define AX_HTTP_FORBIDDEN 403 //You need permission to access that resource.
AxedaCorp 0:65004368569c 15 #define AX_HTTP_NOT_FOUND 404 //The resource was not found
AxedaCorp 0:65004368569c 16 #define AX_HTTP_REQ_INVALID 405 //Content type is invalid, should be application/json most of the time
AxedaCorp 0:65004368569c 17 #define AX_HTTP_CONTENT_INVALID 422 //The Request is valid syntactically but the content is invalid e.g. wrong instruction
AxedaCorp 0:65004368569c 18 #define AX_HTTP_SERVER_ERROR 500 //The server attempted to handle your request but a failure occurred; check the server logs.
AxedaCorp 0:65004368569c 19
AxedaCorp 0:65004368569c 20 #define HTTPKEY_GET 0
AxedaCorp 0:65004368569c 21 #define HTTPKEY_POST 1
AxedaCorp 0:65004368569c 22 #define HTTPKEY_PUT 2
AxedaCorp 0:65004368569c 23 #define HTTPKEY_PROTOCOL 3
AxedaCorp 0:65004368569c 24 #define HTTPKEY_CRLF 4
AxedaCorp 0:65004368569c 25
AxedaCorp 0:65004368569c 26
AxedaCorp 0:65004368569c 27 #define HTTPH_HOST 0
AxedaCorp 0:65004368569c 28 #define HTTPH_TYPE 1
AxedaCorp 0:65004368569c 29 #define HTTPH_LENGTH 2
AxedaCorp 0:65004368569c 30 #define HTTPH_CONNECTION 3
AxedaCorp 0:65004368569c 31 #define HTTPH_XCOUNT 4
AxedaCorp 0:65004368569c 32 #define HTTPH_CONTENTDIS 5
AxedaCorp 0:65004368569c 33 #define HTTPH_MULTIPART 6
AxedaCorp 0:65004368569c 34 #define HTTPH_FORMDATA 7
AxedaCorp 0:65004368569c 35 #define HTTPH_ACCEPT 8
AxedaCorp 0:65004368569c 36
AxedaCorp 0:65004368569c 37 #define HTTPR_TYPE 0
AxedaCorp 0:65004368569c 38 #define HTTPR_LENGTH 1
AxedaCorp 0:65004368569c 39 #define HTTPR_BODY_SEP 2
AxedaCorp 0:65004368569c 40 #define HTTPR_DELIMS 3
AxedaCorp 0:65004368569c 41
AxedaCorp 0:65004368569c 42 int http_debug=AX_FALSE;
AxedaCorp 0:65004368569c 43 int x_count=0;
AxedaCorp 0:65004368569c 44
AxedaCorp 0:65004368569c 45 int http_getResponse(HTTP_Transmission *response, ax_socket *source);
AxedaCorp 0:65004368569c 46
AxedaCorp 0:65004368569c 47 char *HTTP_KEY[]= {"GET", "POST", "PUT", "HTTP/1.1", "\r\n"};
AxedaCorp 0:65004368569c 48 char *HTTP_HEADER[] = { "Host: ", "Content-Type: ", "Content-Length: ", "Connection: close", "x-count: ", "Content-Disposition: ","multipart/form-data; boundary=", "form-data;", "Accept: */*"};
AxedaCorp 0:65004368569c 49 char *HTTP_RESP[] = { "Content-Type:", "Content-Length:", "\r\n\r\n", " \r\n"};
AxedaCorp 0:65004368569c 50
AxedaCorp 0:65004368569c 51 int http_send(HTTP_Transmission *request, char *host, int port, int secure, HTTP_Transmission *response){
AxedaCorp 0:65004368569c 52 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 53
AxedaCorp 0:65004368569c 54 switch(request->operation) {
AxedaCorp 0:65004368569c 55 case AX_HTTP_GET:
AxedaCorp 0:65004368569c 56 retVal=http_send_get(request->resource, host, port, secure, response);
AxedaCorp 0:65004368569c 57 break;
AxedaCorp 0:65004368569c 58 case AX_HTTP_POST:
AxedaCorp 0:65004368569c 59 retVal=http_send_post(request->resource, host, port, secure, &request->headers, request->body, request->body_length, response);
AxedaCorp 0:65004368569c 60 break;
AxedaCorp 0:65004368569c 61 case AX_HTTP_PUT:
AxedaCorp 0:65004368569c 62 retVal=http_send_put(request->resource, host, port, secure, &request->headers, request->body, request->body_length, response);
AxedaCorp 0:65004368569c 63 break;
AxedaCorp 0:65004368569c 64 case AX_HTTP_MPOST:
AxedaCorp 0:65004368569c 65 retVal=http_send_mpost(request->resource, host, port, secure, &request->headers, request->body, request->body_length, request->mpData, 1, response);
AxedaCorp 0:65004368569c 66 break;
AxedaCorp 0:65004368569c 67 }
AxedaCorp 0:65004368569c 68
AxedaCorp 0:65004368569c 69 return retVal;
AxedaCorp 0:65004368569c 70 }
AxedaCorp 0:65004368569c 71
AxedaCorp 0:65004368569c 72 int http_send_get(char *resource, char *host, int port, int secure, HTTP_Transmission *response) {
AxedaCorp 0:65004368569c 73 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 74
AxedaCorp 0:65004368569c 75 if(!resource||!host||!response) { return AX_ARGNULL; }
AxedaCorp 0:65004368569c 76 if((port < 0)||(port>65536)) { return AX_NET_PORT_INVALID; }
AxedaCorp 0:65004368569c 77 int sz=strlen(HTTP_KEY[HTTPKEY_GET])+strlen(resource)+strlen(HTTP_KEY[HTTPKEY_PROTOCOL])+strlen(HTTP_HEADER[HTTPH_HOST])+strlen(host)+13+6+strlen(HTTP_HEADER[HTTPH_CONNECTION])+strlen(HTTP_HEADER[HTTPH_ACCEPT]); //+6 adds space for extra CRLFs, spaces, and null terminator
AxedaCorp 0:65004368569c 78
AxedaCorp 0:65004368569c 79 ax_socket sock;
AxedaCorp 0:65004368569c 80
AxedaCorp 0:65004368569c 81 retVal=net_socketInit(&sock);
AxedaCorp 0:65004368569c 82 retVal=net_socketOpen(&sock, host, port, secure);
AxedaCorp 0:65004368569c 83 if(retVal!=AX_OK){
AxedaCorp 0:65004368569c 84 return retVal;
AxedaCorp 0:65004368569c 85 }
AxedaCorp 0:65004368569c 86
AxedaCorp 0:65004368569c 87 char *buff=(char *)malloc(sizeof(char)*sz);
AxedaCorp 0:65004368569c 88 int wrtSz=snprintf(buff, sz, "%s %s %s%s%s%s:%d%s%s%s%s", HTTP_KEY[HTTPKEY_GET], resource, HTTP_KEY[HTTPKEY_PROTOCOL], HTTP_KEY[HTTPKEY_CRLF], HTTP_HEADER[HTTPH_HOST], host, port, HTTP_KEY[HTTPKEY_CRLF], HTTP_HEADER[HTTPH_CONNECTION], HTTP_KEY[HTTPKEY_CRLF], HTTP_KEY[HTTPKEY_CRLF]);
AxedaCorp 0:65004368569c 89
AxedaCorp 0:65004368569c 90 if(wrtSz>sz) { printDebug("http_send_get(): Buffer allocated for header was too small, truncated"); }
AxedaCorp 0:65004368569c 91 retVal=net_socketWrite(&sock, buff, sz);
AxedaCorp 0:65004368569c 92 retVal=net_socketFlush(&sock);
AxedaCorp 0:65004368569c 93
AxedaCorp 0:65004368569c 94 retVal=http_getResponse(response, &sock);
AxedaCorp 0:65004368569c 95 if(retVal==AX_HTTP_OK) { retVal=AX_OK; }
AxedaCorp 0:65004368569c 96
AxedaCorp 0:65004368569c 97 net_socketClose(&sock);
AxedaCorp 0:65004368569c 98 free(buff);
AxedaCorp 0:65004368569c 99 return retVal;
AxedaCorp 0:65004368569c 100 }
AxedaCorp 0:65004368569c 101
AxedaCorp 0:65004368569c 102 int http_send_put(char *resource, char *host, int port, int secure, HTTP_Headers *headers, char *data, int data_sz, HTTP_Transmission *response) {
AxedaCorp 0:65004368569c 103 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 104
AxedaCorp 0:65004368569c 105 if(!resource||!host||!response) { return AX_ARGNULL; }
AxedaCorp 0:65004368569c 106 if((port < 0)||(port>65536)) { return AX_NET_PORT_INVALID; }
AxedaCorp 0:65004368569c 107 if((!data)&&(data_sz!=0)) { return AX_CONFLICTING_ARG; }
AxedaCorp 0:65004368569c 108
AxedaCorp 0:65004368569c 109 ax_socket sock;
AxedaCorp 0:65004368569c 110
AxedaCorp 0:65004368569c 111 retVal=net_socketInit(&sock);
AxedaCorp 0:65004368569c 112 retVal=net_socketOpen(&sock, host, port, secure);
AxedaCorp 0:65004368569c 113 if(retVal!=AX_OK){ //if a failure occurred bail out. failures here would be that a port is not free
AxedaCorp 0:65004368569c 114 return retVal;
AxedaCorp 0:65004368569c 115 }
AxedaCorp 0:65004368569c 116 printDebug("Attempting to create headers");
AxedaCorp 0:65004368569c 117 char *header=NULL;
AxedaCorp 0:65004368569c 118 header=createHeaders(header, HTTPKEY_PUT, resource, HTTPKEY_PROTOCOL, host, headers->contentType, data_sz, x_count); //returns a malloc'd string with the headers
AxedaCorp 0:65004368569c 119
AxedaCorp 0:65004368569c 120 retVal=net_socketWrite(&sock, header, strlen(header));
AxedaCorp 0:65004368569c 121 if(retVal!=AX_OK){ //if a failure occurred bail out. failures here would be that a port is not free
AxedaCorp 0:65004368569c 122 goto cleanup;
AxedaCorp 0:65004368569c 123 }
AxedaCorp 0:65004368569c 124 retVal=net_socketWrite(&sock, data, data_sz);
AxedaCorp 0:65004368569c 125 net_socketFlush(&sock);
AxedaCorp 0:65004368569c 126 if(retVal!=AX_OK) {
AxedaCorp 0:65004368569c 127 retVal=AX_NET_ERR_DATA_WRITE;
AxedaCorp 0:65004368569c 128 goto cleanup;
AxedaCorp 0:65004368569c 129 }
AxedaCorp 0:65004368569c 130
AxedaCorp 0:65004368569c 131 retVal=http_getResponse(response, &sock);
AxedaCorp 0:65004368569c 132 if(retVal==AX_HTTP_OK) { retVal=AX_OK; }
AxedaCorp 0:65004368569c 133
AxedaCorp 0:65004368569c 134 x_count++;
AxedaCorp 0:65004368569c 135 goto cleanup;
AxedaCorp 0:65004368569c 136
AxedaCorp 0:65004368569c 137
AxedaCorp 0:65004368569c 138 cleanup:
AxedaCorp 0:65004368569c 139 net_socketClose(&sock);
AxedaCorp 0:65004368569c 140 free(header);
AxedaCorp 0:65004368569c 141 return retVal;
AxedaCorp 0:65004368569c 142 }
AxedaCorp 0:65004368569c 143
AxedaCorp 0:65004368569c 144
AxedaCorp 0:65004368569c 145
AxedaCorp 0:65004368569c 146 int http_send_post(char *resource, char *host, int port, int secure, HTTP_Headers *headers, char *data, int data_sz, HTTP_Transmission *response){
AxedaCorp 0:65004368569c 147 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 148
AxedaCorp 0:65004368569c 149 if(!resource||!host||!response) { return AX_ARGNULL; }
AxedaCorp 0:65004368569c 150 if((port < 0)||(port>65536)) { return AX_NET_PORT_INVALID; }
AxedaCorp 0:65004368569c 151 if((!data)&&(data_sz!=0)) { return AX_CONFLICTING_ARG; }
AxedaCorp 0:65004368569c 152
AxedaCorp 0:65004368569c 153 ax_socket sock;
AxedaCorp 0:65004368569c 154
AxedaCorp 0:65004368569c 155 retVal=net_socketInit(&sock);
AxedaCorp 0:65004368569c 156 retVal=net_socketOpen(&sock, host, port, secure);
AxedaCorp 0:65004368569c 157 if(retVal!=AX_OK){ //if a failure occurred bail out. failures here would be that a port is not free
AxedaCorp 0:65004368569c 158 return retVal;
AxedaCorp 0:65004368569c 159 }
AxedaCorp 0:65004368569c 160 printDebug("Attempting to create headers");
AxedaCorp 0:65004368569c 161 char *header=NULL;
AxedaCorp 0:65004368569c 162 header=createHeaders(header, HTTPKEY_POST, resource, HTTPKEY_PROTOCOL, host, headers->contentType, data_sz, x_count); //returns a malloc'd string with the headers
AxedaCorp 0:65004368569c 163
AxedaCorp 0:65004368569c 164 retVal=net_socketWrite(&sock, header, strlen(header));
AxedaCorp 0:65004368569c 165 if(retVal!=AX_OK){ //if a failure occurred bail out. failures here would be that a port is not free
AxedaCorp 0:65004368569c 166 goto cleanup;
AxedaCorp 0:65004368569c 167 }
AxedaCorp 0:65004368569c 168 retVal=net_socketWrite(&sock, data, data_sz);
AxedaCorp 0:65004368569c 169 net_socketFlush(&sock);
AxedaCorp 0:65004368569c 170 x_count++;
AxedaCorp 0:65004368569c 171 printDebug("Transmission sent, Waiting for response\n");
AxedaCorp 0:65004368569c 172
AxedaCorp 0:65004368569c 173
AxedaCorp 0:65004368569c 174 retVal=http_getResponse(response, &sock);
AxedaCorp 0:65004368569c 175 if(retVal==AX_HTTP_OK) { retVal=AX_OK; }
AxedaCorp 0:65004368569c 176
AxedaCorp 0:65004368569c 177 goto cleanup;
AxedaCorp 0:65004368569c 178
AxedaCorp 0:65004368569c 179 cleanup:
AxedaCorp 0:65004368569c 180 net_socketClose(&sock);
AxedaCorp 0:65004368569c 181 free(header);
AxedaCorp 0:65004368569c 182 return retVal;
AxedaCorp 0:65004368569c 183 }
AxedaCorp 0:65004368569c 184
AxedaCorp 0:65004368569c 185 int http_send_mpost(char *resource, char *host, int port, int secure, HTTP_Headers *headers, char *data, int data_sz, HTTP_Part *files, int part_ct, HTTP_Transmission *response) {
AxedaCorp 0:65004368569c 186 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 187 // char xcount_tmp[10];
AxedaCorp 0:65004368569c 188 // char clength_tmp[10];
AxedaCorp 0:65004368569c 189 int boundary_sz=10;
AxedaCorp 0:65004368569c 190 int body_sz=0;
AxedaCorp 0:65004368569c 191 int hint_sz=0;
AxedaCorp 0:65004368569c 192 char *hint=NULL;
AxedaCorp 0:65004368569c 193 char *header=NULL;
AxedaCorp 0:65004368569c 194 ax_socket sock;
AxedaCorp 0:65004368569c 195
AxedaCorp 0:65004368569c 196 if(!resource||!host||!response) { return AX_ARGNULL; }
AxedaCorp 0:65004368569c 197 if((port < 0)||(port>65536)) { return AX_NET_PORT_INVALID; }
AxedaCorp 0:65004368569c 198 if((!data)&&(data_sz!=0)) { return AX_CONFLICTING_ARG; }
AxedaCorp 0:65004368569c 199 //Assemble body Parts
AxedaCorp 0:65004368569c 200 char *fileargs=NULL;
AxedaCorp 0:65004368569c 201 int file_arg_len=strlen(files->file_name)+51;
AxedaCorp 0:65004368569c 202 fileargs=(char *)malloc(sizeof(char)*file_arg_len);
AxedaCorp 0:65004368569c 203 int wrtfarg=snprintf(fileargs, file_arg_len, "name=\"file\"; filename=\"%s\"\r\nContent-Type: text/plain", files->file_name);
AxedaCorp 0:65004368569c 204 if(wrtfarg>file_arg_len) { printDebug("http_send_mpost(): Buffer allocated for file arguments was too small, truncated"); }
AxedaCorp 0:65004368569c 205 if(files->hint!=NULL)
AxedaCorp 0:65004368569c 206 {
AxedaCorp 0:65004368569c 207 hint_sz=strlen(files->hint)+18;//+1 for terminating char
AxedaCorp 0:65004368569c 208 hint=(char *)malloc(sizeof(char)*hint_sz);
AxedaCorp 0:65004368569c 209 int wrthint=snprintf(hint, hint_sz, "name=\"hint\"\r\n\r\n%s\r\n", files->hint);
AxedaCorp 0:65004368569c 210 if(wrthint>hint_sz) { printDebug("http_send_mpost(): Buffer allocated for hint was too small, truncated"); }
AxedaCorp 0:65004368569c 211 }
AxedaCorp 0:65004368569c 212 //body size calculation here
AxedaCorp 0:65004368569c 213 //Boundary: --<boundary>
AxedaCorp 0:65004368569c 214 body_sz=body_sz+2+boundary_sz;
AxedaCorp 0:65004368569c 215 //adding body ---for mparts
AxedaCorp 0:65004368569c 216 if(data_sz>0){
AxedaCorp 0:65004368569c 217 body_sz=body_sz+36+data_sz+boundary_sz;
AxedaCorp 0:65004368569c 218 }
AxedaCorp 0:65004368569c 219 //Add Hint Content-Disposition: form-data;name="<hint>"\r\n--<boundary>\r\n
AxedaCorp 0:65004368569c 220 if(files->hint!=NULL){
AxedaCorp 0:65004368569c 221 body_sz=body_sz+38+strlen(hint)+boundary_sz;
AxedaCorp 0:65004368569c 222 }
AxedaCorp 0:65004368569c 223 if(files->data_sz>0) {
AxedaCorp 0:65004368569c 224 body_sz=body_sz+38+strlen(fileargs)+files->data_sz;
AxedaCorp 0:65004368569c 225 }
AxedaCorp 0:65004368569c 226 body_sz=body_sz+8+boundary_sz;
AxedaCorp 0:65004368569c 227
AxedaCorp 0:65004368569c 228 //get a boundary for the multipart
AxedaCorp 0:65004368569c 229 char *boundary=(char *)malloc(sizeof(char)*boundary_sz);
AxedaCorp 0:65004368569c 230 http_getBoundary(boundary, boundary_sz);
AxedaCorp 0:65004368569c 231 //convert the data size integer into a string
AxedaCorp 0:65004368569c 232 // int wrtCLen=snprintf(clength_tmp, 10, "%d", body_sz);
AxedaCorp 0:65004368569c 233 // if(wrtCLen>10) { printDebug("http_send_mpost(): Buffer allocated for clength_tmp was too small, truncated"); }
AxedaCorp 0:65004368569c 234 //Add operation line
AxedaCorp 0:65004368569c 235
AxedaCorp 0:65004368569c 236 printDebug("Attempting to create headers");
AxedaCorp 0:65004368569c 237
AxedaCorp 0:65004368569c 238 //Create Content Type
AxedaCorp 0:65004368569c 239 int cTypeSz=0;
AxedaCorp 0:65004368569c 240 cTypeSz=strlen(HTTP_HEADER[HTTPH_MULTIPART])+strlen(boundary)+1;
AxedaCorp 0:65004368569c 241 char *contentType=(char *)calloc(cTypeSz, sizeof(char));
AxedaCorp 0:65004368569c 242 int wrt_ctype=snprintf(contentType, cTypeSz, "%s%s", HTTP_HEADER[HTTPH_MULTIPART], boundary);
AxedaCorp 0:65004368569c 243 if(wrt_ctype>cTypeSz) { printDebug("http_send_mpost(): Buffer allocated for contentType was too small, truncated");}
AxedaCorp 0:65004368569c 244 //Get the headers
AxedaCorp 0:65004368569c 245 header=createHeaders(header, HTTPKEY_POST, resource, HTTPKEY_PROTOCOL, host, contentType, body_sz, x_count); //returns a malloc'd string with the headers
AxedaCorp 0:65004368569c 246 free(contentType); //we're done with the string we used to cat the values
AxedaCorp 0:65004368569c 247 //Start to send the data
AxedaCorp 0:65004368569c 248 retVal=net_socketInit(&sock);
AxedaCorp 0:65004368569c 249 retVal=net_socketOpen(&sock, host, port, secure);
AxedaCorp 0:65004368569c 250 if(retVal!=AX_OK){ //if a failure occurred, bail out. A failure here would indicate that a port is closed, or the network is not available.
AxedaCorp 0:65004368569c 251 return retVal;
AxedaCorp 0:65004368569c 252 }
AxedaCorp 0:65004368569c 253 //Write the headers
AxedaCorp 0:65004368569c 254 retVal=net_socketWrite(&sock, header, strlen(header));
AxedaCorp 0:65004368569c 255 if(retVal!=AX_OK){ goto cleanup; }
AxedaCorp 0:65004368569c 256
AxedaCorp 0:65004368569c 257 //write the body...
AxedaCorp 0:65004368569c 258 //start boundary
AxedaCorp 0:65004368569c 259 retVal=net_socketWrite(&sock, "--", 2);
AxedaCorp 0:65004368569c 260 retVal=net_socketWrite(&sock, boundary, boundary_sz);
AxedaCorp 0:65004368569c 261 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 262 //1st section -uses the data in body if specified
AxedaCorp 0:65004368569c 263 if(data_sz>0){
AxedaCorp 0:65004368569c 264 retVal=net_socketWrite(&sock, HTTP_HEADER[HTTPH_CONTENTDIS], 21);
AxedaCorp 0:65004368569c 265 retVal=net_socketWrite(&sock, HTTP_HEADER[HTTPH_FORMDATA], 11);
AxedaCorp 0:65004368569c 266 retVal=net_socketWrite(&sock, data, data_sz); //Write body data, the fields and values
AxedaCorp 0:65004368569c 267 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 268 retVal=net_socketWrite(&sock, "--", 2);
AxedaCorp 0:65004368569c 269 retVal=net_socketWrite(&sock, boundary, boundary_sz);
AxedaCorp 0:65004368569c 270 }
AxedaCorp 0:65004368569c 271 if(files->hint!=NULL){
AxedaCorp 0:65004368569c 272 retVal=net_socketWrite(&sock, HTTP_HEADER[HTTPH_CONTENTDIS], 21);
AxedaCorp 0:65004368569c 273 retVal=net_socketWrite(&sock, HTTP_HEADER[HTTPH_FORMDATA], 11);
AxedaCorp 0:65004368569c 274 retVal=net_socketWrite(&sock, hint, hint_sz); //Write body data, the fields and values
AxedaCorp 0:65004368569c 275 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 276 retVal=net_socketWrite(&sock, "--", 2);
AxedaCorp 0:65004368569c 277 retVal=net_socketWrite(&sock, boundary, boundary_sz);
AxedaCorp 0:65004368569c 278 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 279 }
AxedaCorp 0:65004368569c 280 if(files->data_sz>0) {
AxedaCorp 0:65004368569c 281 retVal=net_socketWrite(&sock, HTTP_HEADER[HTTPH_CONTENTDIS], 21);
AxedaCorp 0:65004368569c 282 retVal=net_socketWrite(&sock, HTTP_HEADER[HTTPH_FORMDATA], 11);
AxedaCorp 0:65004368569c 283 retVal=net_socketWrite(&sock, fileargs, file_arg_len);
AxedaCorp 0:65004368569c 284 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 285 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 286 retVal=net_socketWrite(&sock, (char *)files->data, files->data_sz);
AxedaCorp 0:65004368569c 287 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 288 }
AxedaCorp 0:65004368569c 289 retVal=net_socketWrite(&sock, "--", 2);
AxedaCorp 0:65004368569c 290 retVal=net_socketWrite(&sock, boundary, boundary_sz);
AxedaCorp 0:65004368569c 291 retVal=net_socketWrite(&sock, "--", 2);
AxedaCorp 0:65004368569c 292 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 293 retVal=net_socketWrite(&sock, HTTP_KEY[HTTPKEY_CRLF], 2);
AxedaCorp 0:65004368569c 294
AxedaCorp 0:65004368569c 295
AxedaCorp 0:65004368569c 296 net_socketFlush(&sock);
AxedaCorp 0:65004368569c 297 x_count++;
AxedaCorp 0:65004368569c 298 retVal=http_getResponse(response, &sock);
AxedaCorp 0:65004368569c 299 if(retVal==AX_HTTP_OK) { retVal=AX_OK; }
AxedaCorp 0:65004368569c 300
AxedaCorp 0:65004368569c 301 cleanup:
AxedaCorp 0:65004368569c 302 net_socketClose(&sock);
AxedaCorp 0:65004368569c 303 free(header);
AxedaCorp 0:65004368569c 304 free(hint);
AxedaCorp 0:65004368569c 305 free(fileargs);
AxedaCorp 0:65004368569c 306 free(boundary);
AxedaCorp 0:65004368569c 307 return retVal;
AxedaCorp 0:65004368569c 308 }
AxedaCorp 0:65004368569c 309
AxedaCorp 0:65004368569c 310 void http_add_mpart(HTTP_Transmission *request, char *file_name, unsigned char *data, int file_sz) {
AxedaCorp 0:65004368569c 311 //For future expansion.
AxedaCorp 0:65004368569c 312 }
AxedaCorp 0:65004368569c 313
AxedaCorp 0:65004368569c 314 //Return Handling
AxedaCorp 0:65004368569c 315 HTTP_Transmission *http_parse(HTTP_Transmission *response, char *data){
AxedaCorp 0:65004368569c 316 printDebug("Starting parse of ");
AxedaCorp 0:65004368569c 317 printDebug(data);
AxedaCorp 0:65004368569c 318
AxedaCorp 0:65004368569c 319 char clength[7];
AxedaCorp 0:65004368569c 320 int reqSize=0;
AxedaCorp 0:65004368569c 321 char *temp=NULL;
AxedaCorp 0:65004368569c 322 char *bodyStart=NULL;
AxedaCorp 0:65004368569c 323 response->body_length=0;
AxedaCorp 0:65004368569c 324 response->response_code=-1; //indicate that no response code has been recieved(yet)
AxedaCorp 0:65004368569c 325
AxedaCorp 0:65004368569c 326 reqSize=strlen(data);
AxedaCorp 0:65004368569c 327 if(reqSize<=12) { printDebug("No Body included"); }
AxedaCorp 0:65004368569c 328 else { printDebug("body present" );
AxedaCorp 0:65004368569c 329 bodyStart=strstr(data, HTTP_RESP[HTTPR_BODY_SEP]);
AxedaCorp 0:65004368569c 330 if(bodyStart==NULL) { printDebug("D'oh! Couldn't parse for body\n"); }
AxedaCorp 0:65004368569c 331 else {
AxedaCorp 0:65004368569c 332 int body_length=strlen(bodyStart)-4; //don't count the extra \r\n\r\n
AxedaCorp 0:65004368569c 333 response->body_length=body_length;
AxedaCorp 0:65004368569c 334 response->body=(char *)calloc(body_length+1, sizeof(char));
AxedaCorp 0:65004368569c 335 snprintf(response->body, body_length+1, "%s", bodyStart+4); //the +4 gets rid of the preceeding \r\n\r\n
AxedaCorp 0:65004368569c 336 }
AxedaCorp 0:65004368569c 337 }
AxedaCorp 0:65004368569c 338
AxedaCorp 0:65004368569c 339 temp = strtok(data, HTTP_RESP[HTTPR_DELIMS]);
AxedaCorp 0:65004368569c 340 while (temp!=NULL) {
AxedaCorp 0:65004368569c 341
AxedaCorp 0:65004368569c 342 if((strlen(temp)==3)&&(isdigit((int)temp[0]))&&(isdigit((int)temp[1]))&&(isdigit((int)temp[2]))) {
AxedaCorp 0:65004368569c 343 response->response_code=atoi(temp); //convert the text response code to an int.
AxedaCorp 0:65004368569c 344 }
AxedaCorp 0:65004368569c 345
AxedaCorp 0:65004368569c 346 if(strcmp(temp, HTTP_RESP[HTTPR_TYPE])==0) {
AxedaCorp 0:65004368569c 347 response->headers.contentType=strtok(NULL, HTTP_RESP[HTTPR_DELIMS]);
AxedaCorp 0:65004368569c 348 }
AxedaCorp 0:65004368569c 349 if(strcmp(temp, HTTP_RESP[HTTPR_LENGTH])==0){
AxedaCorp 0:65004368569c 350 temp=strtok(NULL, HTTP_RESP[HTTPR_DELIMS]);
AxedaCorp 0:65004368569c 351 strcpy(clength, temp);
AxedaCorp 0:65004368569c 352 response->body_length=atoi(clength);
AxedaCorp 0:65004368569c 353 if(response->body_length<=0) {
AxedaCorp 0:65004368569c 354 response->body_length=strlen(bodyStart+4);
AxedaCorp 0:65004368569c 355 }
AxedaCorp 0:65004368569c 356 }
AxedaCorp 0:65004368569c 357
AxedaCorp 0:65004368569c 358 temp=strtok(NULL, HTTP_RESP[HTTPR_DELIMS]);
AxedaCorp 0:65004368569c 359 }
AxedaCorp 0:65004368569c 360
AxedaCorp 0:65004368569c 361 return response;
AxedaCorp 0:65004368569c 362 }
AxedaCorp 0:65004368569c 363
AxedaCorp 0:65004368569c 364 int http_getResponse(HTTP_Transmission *response, ax_socket *source){
AxedaCorp 0:65004368569c 365 int retVal=AX_UNKNOWN;
AxedaCorp 0:65004368569c 366 char *responseTxt=NULL;
AxedaCorp 0:65004368569c 367 responseTxt=(char *)calloc(AX_NET_BUFF_S, sizeof(char));
AxedaCorp 0:65004368569c 368 int length=0;
AxedaCorp 0:65004368569c 369 response->response_code=0;
AxedaCorp 0:65004368569c 370 retVal=net_socketRead(source, 300, (unsigned char *)responseTxt, AX_NET_BUFF_S, &length);
AxedaCorp 0:65004368569c 371 if(retVal==AX_OK) { printDebug("response received"); }
AxedaCorp 0:65004368569c 372 else { printDebug("Response Timeout"); return AX_NET_ERR_TIMEOUT;}
AxedaCorp 0:65004368569c 373 http_parse(response, (char *)responseTxt);
AxedaCorp 0:65004368569c 374
AxedaCorp 0:65004368569c 375 free(responseTxt);
AxedaCorp 0:65004368569c 376 return response->response_code;
AxedaCorp 0:65004368569c 377 }
AxedaCorp 0:65004368569c 378
AxedaCorp 0:65004368569c 379
AxedaCorp 0:65004368569c 380 //Supporting Methods
AxedaCorp 0:65004368569c 381 /***************************************************************************************************/
AxedaCorp 0:65004368569c 382 /*http_getBoundary() */
AxedaCorp 0:65004368569c 383 /* */
AxedaCorp 0:65004368569c 384 /*This method creates a unique string token that is used when creating mulitpart form transmissions*/
AxedaCorp 0:65004368569c 385 /*in HTTP. It will return a random sequence of letters and numbers. */
AxedaCorp 0:65004368569c 386 /***************************************************************************************************/
AxedaCorp 0:65004368569c 387 char *http_getBoundary(char *buff, int length){
AxedaCorp 0:65004368569c 388 int ctr=0;
AxedaCorp 0:65004368569c 389 if(buff==NULL) { return NULL; } //if no pointer was given to us give a nothing back.
AxedaCorp 0:65004368569c 390 if(length<=0) {return NULL;}
AxedaCorp 0:65004368569c 391 int randNum=0;
AxedaCorp 0:65004368569c 392 for(ctr=0; ctr<length; ctr++) {
AxedaCorp 0:65004368569c 393 randNum=abs(randInt()%62);
AxedaCorp 0:65004368569c 394 if(randNum<=10) {
AxedaCorp 0:65004368569c 395 if(randNum==0) { randNum++; } //avoid using slashes in boundary, char 47
AxedaCorp 0:65004368569c 396 buff[ctr]=randNum+47;
AxedaCorp 0:65004368569c 397 }
AxedaCorp 0:65004368569c 398 else if((randNum>10)&&(randNum<=36)) {
AxedaCorp 0:65004368569c 399 buff[ctr]=(randNum-10)+64;
AxedaCorp 0:65004368569c 400 }
AxedaCorp 0:65004368569c 401 else {
AxedaCorp 0:65004368569c 402 buff[ctr]=(randNum-37)+97;
AxedaCorp 0:65004368569c 403 }
AxedaCorp 0:65004368569c 404 if(buff[ctr]==32) {buff[ctr]=97; }
AxedaCorp 0:65004368569c 405 }
AxedaCorp 0:65004368569c 406
AxedaCorp 0:65004368569c 407 return buff;
AxedaCorp 0:65004368569c 408 }
AxedaCorp 0:65004368569c 409
AxedaCorp 0:65004368569c 410 void zeroOutHTTPXmission(HTTP_Transmission *tgt){
AxedaCorp 0:65004368569c 411
AxedaCorp 0:65004368569c 412 tgt->operation=-1;
AxedaCorp 0:65004368569c 413 tgt->resource=NULL;
AxedaCorp 0:65004368569c 414 tgt->headers.contentType=NULL;
AxedaCorp 0:65004368569c 415 tgt->body=NULL;
AxedaCorp 0:65004368569c 416 tgt->body_length=0;
AxedaCorp 0:65004368569c 417 tgt->mpData=NULL;
AxedaCorp 0:65004368569c 418 tgt->response_code=-1;
AxedaCorp 0:65004368569c 419 tgt->secure=-1;
AxedaCorp 0:65004368569c 420 }
AxedaCorp 0:65004368569c 421
AxedaCorp 0:65004368569c 422 char *createHeaders(char *tgtBuff, int http_operation, char *resource, int httpver, char *hostname, char *contentType, int contentLength, int xCount) {
AxedaCorp 0:65004368569c 423 int headerSz=4; //we account for the closing CRLF CRLF here
AxedaCorp 0:65004368569c 424 char clength_tmp[10];
AxedaCorp 0:65004368569c 425 char xCount_tmp[10];
AxedaCorp 0:65004368569c 426 int wrthdr=0;
AxedaCorp 0:65004368569c 427 //convert the ints to strings so we can get an strlen on them
AxedaCorp 0:65004368569c 428 snprintf(clength_tmp, 10, "%d", contentLength);
AxedaCorp 0:65004368569c 429 snprintf(xCount_tmp, 10, "%d", xCount);
AxedaCorp 0:65004368569c 430
AxedaCorp 0:65004368569c 431 //String Size Calculations
AxedaCorp 0:65004368569c 432 headerSz=headerSz+strlen(resource)+strlen(HTTP_KEY[http_operation])+strlen(HTTP_KEY[httpver])+5; //"POST HTTP/1.1" there are 2 spaces and a CRLF in there as well., 1 for the null
AxedaCorp 0:65004368569c 433 headerSz=headerSz+strlen(HTTP_HEADER[HTTPH_HOST])+strlen(hostname)+2; //Host: <hostname>
AxedaCorp 0:65004368569c 434 headerSz=headerSz+strlen(HTTP_HEADER[HTTPH_TYPE])+strlen(contentType)+2; //Content-Type: <contentType>
AxedaCorp 0:65004368569c 435 headerSz=headerSz+strlen(HTTP_HEADER[HTTPH_LENGTH])+strlen(clength_tmp)+2; //Content-Length: <contentLength>
AxedaCorp 0:65004368569c 436 headerSz=headerSz+strlen(HTTP_HEADER[HTTPH_CONNECTION])+2; //Connection: Close
AxedaCorp 0:65004368569c 437 if(xCount>0) {
AxedaCorp 0:65004368569c 438 headerSz=headerSz+strlen(HTTP_HEADER[HTTPH_XCOUNT])+strlen(xCount_tmp)+2;
AxedaCorp 0:65004368569c 439 }
AxedaCorp 0:65004368569c 440
AxedaCorp 0:65004368569c 441 //String buffer creation
AxedaCorp 0:65004368569c 442 tgtBuff=(char *)calloc(headerSz, sizeof(char));
AxedaCorp 0:65004368569c 443 //Stuffin the buffer
AxedaCorp 0:65004368569c 444 if((xCount>0)&&(http_debug==AX_TRUE)) {
AxedaCorp 0:65004368569c 445 wrthdr=snprintf(tgtBuff, headerSz, "%s %s %s\r\n%s%s\r\n%s%s\r\n%s%s\r\n%s\r\n%s%s\r\n\r\n", HTTP_KEY[http_operation], resource, HTTP_KEY[httpver], HTTP_HEADER[HTTPH_HOST], hostname, HTTP_HEADER[HTTPH_TYPE], contentType, HTTP_HEADER[HTTPH_LENGTH], clength_tmp, HTTP_HEADER[HTTPH_CONNECTION], HTTP_HEADER[HTTPH_XCOUNT], xCount_tmp);
AxedaCorp 0:65004368569c 446 }
AxedaCorp 0:65004368569c 447 else {
AxedaCorp 0:65004368569c 448 wrthdr=snprintf(tgtBuff, headerSz, "%s %s %s\r\n%s%s\r\n%s%s\r\n%s%s\r\n%s\r\n\r\n", HTTP_KEY[http_operation], resource, HTTP_KEY[httpver], HTTP_HEADER[HTTPH_HOST], hostname, HTTP_HEADER[HTTPH_TYPE], contentType, HTTP_HEADER[HTTPH_LENGTH], clength_tmp, HTTP_HEADER[HTTPH_CONNECTION]);
AxedaCorp 0:65004368569c 449
AxedaCorp 0:65004368569c 450 }
AxedaCorp 0:65004368569c 451 if(wrthdr>headerSz) { printDebug("http_send_post(): Buffer allocated for header was too small, truncated"); }
AxedaCorp 0:65004368569c 452
AxedaCorp 0:65004368569c 453 return tgtBuff;
AxedaCorp 0:65004368569c 454 }
AxedaCorp 0:65004368569c 455
AxedaCorp 0:65004368569c 456
AxedaCorp 0:65004368569c 457
AxedaCorp 0:65004368569c 458
AxedaCorp 0:65004368569c 459
AxedaCorp 0:65004368569c 460