//404 void resp_error_page( header_out_t* header_out, int status_code ) { if ( status_code >= 400 && status_code <= 507 ) { int ret = resp_defined_error_page( header_out , status_code ); if ( ret == 1 ) { return; } } //get header info header_status_t error_page = get_http_status( status_code ); int datalen = strlen( error_page.data ); //header append //resp_append_header( header_out , HEADER_STATUS , error_page.status ); //resp_append_header( header_out , HEADER_SERVER ); create_common_header( header_out , status_code ); header_append_length( header_out , datalen ); resp_append_header( header_out , HEADER_END_LINE ); //send http_response_write( header_out->req->connfd, header_out->data , header_out->length ); http_response_write( header_out->req->connfd, error_page.data , datalen ); http_close( header_out->req , 0 ); }
/* ******************************************************* * Read swapping statistics from haproxy.stat & 80 port ******************************************************* */ static void read_haproxy(struct module *mod) { int i, pos=0; char buf[512]; memset(&st_haproxy, 0, sizeof(struct stats_haproxy)); for (i = 0;i < RETRY;i++) { if (get_http_status() == 0 && access(HAPROXY, 0) == 0) { st_haproxy.stat = 1; break; } } if (st_haproxy.stat == 1 && get_haproxy_detail() == 0) { if (DEBUG) { printf("get right.\n"); } } else { if (DEBUG) { printf("get wrong.\n"); } } if (st_haproxy.stat == 1) { pos = sprintf(buf, HAPROXY_STORE_FMT(DATA_SPLIT), st_haproxy.stat, st_haproxy.uptime, st_haproxy.conns, st_haproxy.qps, st_haproxy.hit, st_haproxy.rt); } buf[pos] = '\0'; set_mod_record(mod, buf); return; }
int recv_http_respond_header(int sockfd) { struct http_respond_header *head; unsigned long data_len; int ret; int code; head = create_respond_header(); if(head == NULL){ ERROR("create respond header error!\n"); return -1; } ret = get_respond_header(sockfd,head); if(ret < 0){ ERROR("Not Found respond header!\n"); return -1; } code = get_http_status(head->respond_buf); if(code != 200){ ERROR("Not support the code!\n"); return -1; } data_len = get_http_length(head->respond_buf); if ( data_len > 0 ) { PJson = init_to_jsonbuf(head, data_len); } release_respond_header(head); return 0; }
/***************************************************************************** * * Exosite_Write * * \param pbuf - string buffer containing data to be sent * bufsize - number of bytes to send * * \return 1 success; 0 failure * * \brief Writes data to Exosite cloud * *****************************************************************************/ int Exosite_Write(char * pbuf, unsigned int bufsize) { int success = 0; int http_status = 0; char bufCIK[41]; char strBuf[10]; if (!exosite_initialized) { status_code = EXO_STATUS_INIT; return success; } if (!Exosite_GetCIK(bufCIK)) { return success; } long sock = connect_to_exosite(); if (sock < 0) { status_code = EXO_STATUS_BAD_TCP; return 0; } // This is an example write POST... // s.send('POST /onep:v1/stack/alias HTTP/1.1\r\n') // s.send('Host: m2.exosite.com\r\n') // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n') // s.send('Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n') // s.send('Content-Length: 6\r\n\r\n') // s.send('temp=2') itoa((int)bufsize, strBuf, 10); //make a string for length sendLine(sock, POSTDATA_LINE, "/onep:v1/stack/alias"); sendLine(sock, HOST_LINE, NULL); sendLine(sock, CIK_LINE, bufCIK); sendLine(sock, CONTENT_LINE, NULL); sendLine(sock, LENGTH_LINE, strBuf); exoHAL_SocketSend(sock, pbuf, bufsize); http_status = get_http_status(sock); exoHAL_SocketClose(sock); if (401 == http_status) { status_code = EXO_STATUS_NOAUTH; } if (204 == http_status) { success = 1; status_code = EXO_STATUS_OK; } return success; }
da_result_t send_user_noti_and_finish_download_flow( int slot_id, char *installed_path, char *etag) { da_result_t ret = DA_RESULT_OK; download_state_t download_state = DA_NULL; da_bool_t need_destroy_download_info = DA_FALSE; DA_LOG_FUNC_START(Default); _da_thread_mutex_lock (&mutex_download_state[slot_id]); download_state = GET_DL_STATE_ON_ID(slot_id); DA_LOG(Default, "state = %d", download_state); _da_thread_mutex_unlock (&mutex_download_state[slot_id]); switch (download_state) { case DOWNLOAD_STATE_FINISH: send_client_finished_info(slot_id, GET_DL_ID(slot_id), installed_path, DA_NULL, DA_RESULT_OK, get_http_status(slot_id)); need_destroy_download_info = DA_TRUE; break; case DOWNLOAD_STATE_CANCELED: send_client_finished_info(slot_id, GET_DL_ID(slot_id), installed_path, etag, DA_RESULT_USER_CANCELED, get_http_status(slot_id)); need_destroy_download_info = DA_TRUE; break; #ifdef PAUSE_EXIT case DOWNLOAD_STATE_PAUSED: need_destroy_download_info = DA_TRUE; break; #endif default: DA_LOG(Default, "download state = %d", download_state); break; } if (need_destroy_download_info == DA_TRUE) { destroy_download_info(slot_id); } else { DA_LOG_CRITICAL(Default, "download info is not destroyed"); } return ret; }
//this function only process status=200 page, not include resp_error_page void create_common_header( header_out_t* header_out, int status_code ) { header_status_t error_page = get_http_status( status_code ); //header append resp_append_header( header_out , HEADER_STATUS , error_page.status ); resp_append_header( header_out , HEADER_SERVER ); resp_append_header( header_out , HEADER_CONTENT_TYPE ); resp_append_header( header_out , HEADER_KEEP_ALIVE ); }
int Exosite_SyncTime() { char day[11]; char time[9]; int http_status = 0; int strLen; char *testStr = "GET /ip HTTP/1.1\r\n"; unsigned char serverAddr[6] = {54, 183, 115, 21, 0, 80}; char strBuf[RX_SIZE]; DateTime datetime; if (!exosite_initialized) { return -1; } long sock = connect_to_exosite_with_server_addr("", serverAddr); if (sock < 0) { return -1; } exoHAL_SocketSend(sock, testStr, strlen(testStr)); sendLine(sock, HOST_LINE, NULL); sendLine(sock, ACCEPT_LINE, "\r\n"); http_status = get_http_status(sock); if (200 != http_status) return -1; strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); exoHAL_SocketClose(sock); if(strLen <= 0) return -1; if(parse_datetime(strBuf, strLen, &datetime) < 0) return -1; sprintf(day, "%s/%02d/%s", datetime.dayStr, month_to_num(datetime.monStr), datetime.yearStr); sprintf(time, "%s:%s:%s", datetime.hourStr, datetime.minStr, datetime.secStr); if(AtLibGs_SetTime(day, time) != ATLIBGS_MSG_ID_OK) return -1; return 0; }
void http_redirect( httpHeader* reqHeader , char* uri ) { header_out_t header_out; memset( &header_out , 0 , sizeof( header_out )); header_out.req = reqHeader; header_status_t error_page = get_http_status( 301 ); //header append create_common_header( &header_out , 301 ); resp_append_header( &header_out , HEADER_LOCATION , uri ); resp_append_header( &header_out , HEADER_CONTENT_LENGTH , 0 ); resp_append_header( &header_out , HEADER_END_LINE ); http_response_write( reqHeader->connfd , header_out.data , header_out.length ); http_close( reqHeader , 0 ); }
int handle_http_error(int errcode, http_req * req, http_rsp * rsp) { char buffer[256] = {0}; http_status * stt = get_http_status(errcode); WEBLOG_API(); ASSERT(stt); sprintf(buffer, "<HTML><BODY><b>%s</b></BODY></HTML>", stt->s_name); rsp->code = errcode; rsp->body = buffer; http_add_rspoption(rsp, "Content-Type", "text/html"); http_add_rspoption(rsp, "Connection", "close"); WEBLOG_INFO("\n<%s> Response >>>>>>>>>>>\n", __FUNCTION__); http_send_response(rsp); WEBLOG_INFO("\n<%s> Response <<<<<<<<<<<\n", __FUNCTION__); return NG; /* Allways return NG here. */ }
/***************************************************************************** * * Cloud_Read * * \param palias - string, name of the datasource alias to read from * pbuf - read buffer to put the read response into * buflen - size of the input buffer * * \return number of bytes read * * \brief Reads data from Exosite cloud * *****************************************************************************/ int Cloud_Read(char * palias, char * pbuf, unsigned char buflen) { int success = 0; int http_status = 0; unsigned char strLen, len, vlen; char *p, *pcheck; if (!cloud_initialized) { status_code = STATUS_INIT; return success; } long sock = connect_to_cloud(); if (sock < 0) { status_code = STATUS_END; return 0; } sendLine(sock, GETDATA_LINE, palias); sendLine(sock, HOST_LINE, NULL); sendLine(sock, CIK_LINE, CIK); sendLine(sock, ACCEPT_LINE, "\r\n"); pcheck = palias; vlen = 0; http_status = get_http_status(sock); if (200 == http_status) { char strBuf[RX_SIZE]; unsigned char crlf = 0; do { strLen = IHMS_SocketRecv(sock, strBuf, RX_SIZE); len = strLen; p = strBuf; // Find 4 consecutive \r or \n - should be: \r\n\r\n while (0 < len && 4 > crlf) { if ('\r' == *p || '\n' == *p) { ++crlf; } else { crlf = 0; } ++p; --len; } // The body is "<key>=<value>" if (0 < len && 4 == crlf && buflen > vlen) { // Move past "<key>" while (0 < len && 0 != *pcheck) { if (*pcheck == *p) { ++pcheck; } else { pcheck = palias; } ++p; --len; } // Match '=', we should now have '<key>=' if (0 < len && 0 == *pcheck && '=' == *p) { ++p; --len; } // read in the rest of the body as the value while (0 < len && buflen > vlen) { pbuf[vlen++] = *p++; --len; } } } while (RX_SIZE == strLen); } IHMS_SocketClose(sock); if (0 == http_status) status_code = STATUS_END; if (200 == http_status) { status_code = STATUS_END; } if (204 == http_status) { status_code = STATUS_END; } if (401 == http_status) { status_code = STATUS_END; } return vlen; }
/***************************************************************************** * * Exosite_Read * * \param palias - string, name of the datasource alias to read from * pbuf - read buffer to put the read response into * buflen - size of the input buffer * * \return number of bytes read * * \brief Reads data from Exosite cloud * *****************************************************************************/ int Exosite_Read(char * palias, char * pbuf, unsigned char buflen) { int http_status = 0; unsigned char len, vlen; char *p, *pcheck; char temp[10]; if (!exosite_initialized) { status_code = EXO_STATUS_INIT; return 0; } long sock = connect_to_exosite(); if (sock < 0) { status_code = EXO_STATUS_BAD_TCP; return 0; } // This is an example read GET // s.send('GET /onep:v1/stack/alias?temp HTTP/1.1\r\n') // s.send('Host: m2.exosite.com\r\n') // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n') // s.send('Accept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n') itoa((int)buflen, temp, 10); //make a string for length strLen = strlen(STR_GET_URL); memcpy(strBuf,STR_GET_URL,strLen); memcpy(&strBuf[strLen],palias, strlen(palias)); strLen += strlen(palias); memcpy(&strBuf[strLen],STR_HTTP, strlen(STR_HTTP)); strLen += strlen(STR_HTTP); strBuf[strLen] = 0; send(sock, strBuf, strLen, 0); send(sock, STR_HOST, 22, 0); send(sock, STR_CIK_HEADER, 15, 0); send(sock, USER_CIK, CIK_LENGTH+2, 0); send(sock, STR_ACCEPT, 60, 0); pcheck = palias; vlen = 0; http_status = get_http_status(sock); if (200 == http_status) { unsigned char crlf = 0; do { strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); len = strLen; p = strBuf; // Find 4 consecutive \r or \n - should be: \r\n\r\n while (0 < len && 4 > crlf) { if ('\r' == *p || '\n' == *p) { ++crlf; } else { crlf = 0; } ++p; --len; } // The body is "<key>=<value>" if (0 < len && 4 == crlf && buflen > vlen) { // Move past "<key>" while (0 < len && 0 != *pcheck) { if (*pcheck == *p) { ++pcheck; } else { pcheck = palias; } ++p; --len; } // Match '=', we should now have '<key>=' if (0 < len && 0 == *pcheck && '=' == *p) { ++p; --len; } // read in the rest of the body as the value while (0 < len && buflen > vlen) { pbuf[vlen++] = *p++; --len; } } } while (RX_SIZE == strLen); } exoHAL_SocketClose(sock); if (200 == http_status) { status_code = EXO_STATUS_OK; } if (204 == http_status) { status_code = EXO_STATUS_OK; } if (401 == http_status) { status_code = EXO_STATUS_NOAUTH; } return vlen; }
/***************************************************************************** * * Exosite_Write * * \param pbuf - string buffer containing data to be sent * bufsize - number of bytes to send * * \return 1 success; 0 failure * * \brief Writes data to Exosite cloud * *****************************************************************************/ int Exosite_Write(char * pbuf, unsigned char bufsize) { int success = 0; int http_status = 0; char temp[10]; if (!exosite_initialized) { status_code = EXO_STATUS_INIT; return success; } long sock = connect_to_exosite(); if (sock < 0) { status_code = EXO_STATUS_BAD_TCP; return 0; } // This is an example write POST... // s.send('POST /onep:v1/stack/alias HTTP/1.1\r\n') // s.send('Host: m2.exosite.com\r\n') // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n') // s.send('Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n') // s.send('Content-Length: 6\r\n\r\n') // s.send('temp=2') itoa((int)bufsize, temp, 10); //make a string for length //combine length line in HTTP POST request strLen = strlen(STR_CONTENT_LENGTH); memcpy(strBuf,STR_CONTENT_LENGTH,strLen); memcpy(&strBuf[strLen],temp, strlen(temp)); strLen += strlen(temp); memcpy(&strBuf[strLen],STR_CRLF, 2); strLen += 2; memcpy(&strBuf[strLen],STR_CRLF, 2); strLen += 2; send(sock, STR_POST_HEADER, 36, 0); send(sock, STR_HOST, 22, 0); send(sock, STR_CIK_HEADER, 15, 0); send(sock, USER_CIK, CIK_LENGTH+2, 0); send(sock, STR_CONTENT, 64, 0); send(sock, strBuf, strLen, 0); send(sock, pbuf, bufsize, 0); // exoHAL_SocketSend(sock, STR_POST_HEADER, 36); // exoHAL_SocketSend(sock, STR_HOST, 22); // exoHAL_SocketSend(sock, STR_CIK_HEADER, 15); // exoHAL_SocketSend(sock, USER_CIK, CIK_LENGTH+2); // exoHAL_SocketSend(sock, STR_CONTENT, 64); // exoHAL_SocketSend(sock, strBuf, strLen); // exoHAL_SocketSend(sock, pbuf, bufsize); http_status = get_http_status(sock); exoHAL_SocketClose(sock); if (401 == http_status) { status_code = EXO_STATUS_NOAUTH; } if (204 == http_status) { success = 1; status_code = EXO_STATUS_OK; } return success; }
/***************************************************************************** * * Exosite_Activate * * \param None * * \return 1 - activation success * 0 - activation failure * * \brief Called after Init has been run in the past, but maybe comms were * down and we have to keep trying * *****************************************************************************/ int Exosite_Activate(void) { int length; char temp[5]; int newcik = 0; int http_status = 0; char cmp_ss[18] = "Content-Length: 40"; char *cmp = cmp_ss; if (!exosite_initialized) { status_code = EXO_STATUS_INIT; return newcik; } //update_m2ip(); //check our IP api to see if the old IP is advertising a new one //exoHAL_Updatm2ip(); long sock = connect_to_exosite(); if (sock < 0) { status_code = EXO_STATUS_BAD_TCP; return 0; } // Get activation Serial Number length = strlen(exosite_provision_info); itoa(length, temp, 10); //make a string for length //combine length line in HTTP POST request strLen = strlen(STR_CONTENT_LENGTH); memcpy(strBuf,STR_CONTENT_LENGTH,strLen); memcpy(&strBuf[strLen],temp, strlen(temp)); strLen += strlen(temp); memcpy(&strBuf[strLen],STR_CRLF, 2); strLen += 2; memcpy(&strBuf[strLen],STR_CRLF, 2); strLen += 2; //Socket send HTTP Request send(sock, STR_POST_ACTIVATE, 35, 0); send(sock, STR_HOST, 22, 0); send(sock, STR_CONTENT, 64, 0); send(sock, strBuf, strLen, 0); send(sock, exosite_provision_info, length, 0); http_status = get_http_status(sock); if (200 == http_status) { unsigned char len; unsigned char cik_len_valid = 0; unsigned char cik_ctrl = 0; char *p; unsigned char crlf = 0; unsigned char ciklen = 0; char NCIK[CIK_LENGTH + 3]; do { strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); len = strLen; p = strBuf; // Find 4 consecutive \r or \n - should be: \r\n\r\n while (0 < len && 4 > crlf) { if ('\r' == *p || '\n' == *p) ++crlf; else { crlf = 0; if (*cmp == *p) { // check the cik length from http response cmp++; if (cmp > &cmp_ss[17])// + strlen(cmp_ss)) cik_len_valid = 1; } else cmp = cmp_ss; } ++p; --len; } // The body is the cik if (0 < len && 4 == crlf && CIK_LENGTH > ciklen) { // TODO, be more robust - match Content-Length header value to CIK_LENGTH unsigned char need, part; if (!(cik_len_valid == 1)) // cik length != 40 { status_code = EXO_STATUS_CONFLICT; exoHAL_SocketClose(sock); return newcik; } need = CIK_LENGTH - ciklen; part = need < len ? need : len; strncpy(NCIK + ciklen, p, part); ciklen += part; } } while (RX_SIZE == strLen); if (CIK_LENGTH == ciklen) { NCIK[40] = '\r'; NCIK[41] = '\n'; NCIK[42] = 0; Exosite_SetCIK(NCIK); newcik = 1; } } exoHAL_SocketClose(sock); if (200 == http_status) { status_code = EXO_STATUS_OK; sendString("200"); } if (404 == http_status) status_code = EXO_STATUS_BAD_SN; if (409 == http_status || 408 == http_status) { status_code = EXO_STATUS_CONFLICT; } return newcik; }
int http_send_response(http_rsp * rsp) { CacheHandle cache = NULL_HANDLE; char * datapointer = NULL; int datalength = 0; char strbuf[64] = {0}; char bodybuf[256] = {0}; HashElement * e = NULL; http_status * stt = get_http_status(rsp->code); WEBLOG_API(); ASSERT(stt); if(using_customized_body != rsp->body) { /* if body is empty, make a default body. */ if(!rsp->body) { WEBLOG_VERBOSE("<%s> body empty. add default body.\n", __FUNCTION__); sprintf(bodybuf, "<HTML><BODY><b>%s</b></BODY></HTML>", stt->s_name); rsp->body = bodybuf; http_add_rspoption(rsp, "Content-Type", "text/html"); } /* if Content-Length is not given, try caculating it here. */ if(!HashLookup(rsp->headOptions, "Content-Length")) { if(rsp->body) sprintf(strbuf, sizett, strlen(rsp->body)); else sprintf(strbuf, "%d", 0); http_add_rspoption(rsp, "Content-Length", strbuf); } } /* server info is always present. */ http_add_rspoption(rsp, "Server", SERVER_VER); #ifdef WEBCFG_SUPPORT_HTTPRANGE http_add_rspoption(rsp, "Accept-Ranges", "bytes"); #endif if(NULL_HANDLE == cache) cache = CacheCreate(); ASSERT(NULL_HANDLE != cache); CacheClean(cache); /* send first line! */ CacheWriteString(cache, "HTTP/1.1 "); CacheWriteString(cache, stt->s_code); CacheWriteString(cache, " "); CacheWriteString(cache, stt->s_name); CacheWriteString(cache, "\r\n"); //HashDump(rsp->headOptions); /* send head options! */ e = HashFirst(rsp->headOptions); while(e) { CacheWriteString(cache, e->name); CacheWriteString(cache, ": "); if(e->value.type == eHashString) { CacheWriteString(cache, e->value.data.string); } else if(e->value.type == eHashInt) { sprintf(strbuf, "%d", e->value.data.integer); CacheWriteString(cache, strbuf); } CacheWriteString(cache, "\r\n"); e = HashNext(rsp->headOptions, e); } CacheWriteString(cache, "\r\n"); /* send body */ if(rsp->body && rsp->body != using_customized_body) CacheWriteString(cache, rsp->body); datalength = CacheGetData(cache, &datapointer); ASSERT(datapointer && datalength > 0); WebWrite(rsp->sock, datapointer, datalength); CacheDestroy(cache); return OK; }
/***************************************************************************** * * Exosite_Read * * \param palias - string, name of the datasource alias to read from * pbuf - read buffer to put the read response into * buflen - size of the input buffer * * \return number of bytes read * * \brief Reads data from Exosite cloud * *****************************************************************************/ int Exosite_Read(char * palias, char * pbuf, unsigned int buflen) { // // Modified by Texas Instruments, DGT, changed buflen from unsigned char to // unsigned int. comment out declaration of *pcheck to prevent warnings // created by CAJ changes below. // int success = 0; int http_status = 0; char bufCIK[41]; unsigned char strLen, len, vlen; char *p; //char *pcheck; if (!exosite_initialized) { status_code = EXO_STATUS_INIT; return success; } if (!Exosite_GetCIK(bufCIK)) { return success; } long sock = connect_to_exosite(); if (sock < 0) { status_code = EXO_STATUS_BAD_TCP; return 0; } // This is an example read GET // s.send('GET /onep:v1/stack/alias?temp HTTP/1.1\r\n') // s.send('Host: m2.exosite.com\r\n') // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n') // s.send('Accept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n') sendLine(sock, GETDATA_LINE, palias); sendLine(sock, HOST_LINE, NULL); sendLine(sock, CIK_LINE, bufCIK); sendLine(sock, ACCEPT_LINE, "\r\n"); // // Modified by Texas Instruments DGT comment reference to pcheck no longer // used. See TI CAJ modification below. // //pcheck = palias; vlen = 0; http_status = get_http_status(sock); if (200 == http_status) { char strBuf[RX_SIZE]; unsigned char crlf = 0; do { strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); len = strLen; p = strBuf; // Find 4 consecutive \r or \n - should be: \r\n\r\n while (0 < len && 4 > crlf) { if ('\r' == *p || '\n' == *p) { ++crlf; } else { crlf = 0; } ++p; --len; } // The body is "<key>=<value>" if (0 < len && 4 == crlf && buflen > vlen) { // Code below removed by CAJ. Removing the key works for a single // READ request, but doesn't work if multiple values were requested. // For multiple values, the server is not guaranteed to return every // value in the same order that they were sent. This means that the // caller will need the "key" to be able to determine which value // belongs with which alias. // Move past "<key>" // while (0 < len && 0 != *pcheck) // { // if (*pcheck == *p) // { // ++pcheck; // } // else // { // pcheck = palias; // } // ++p; // --len; // } // // // Match '=', we should now have '<key>=' // if (0 < len && 0 == *pcheck && '=' == *p) // { // ++p; // --len; // } // // read in the rest of the body as the value while (0 < len && buflen > vlen) { pbuf[vlen++] = *p++; --len; } } } while (RX_SIZE == strLen); } exoHAL_SocketClose(sock); if (200 == http_status) { status_code = EXO_STATUS_OK; } if (204 == http_status) { status_code = EXO_STATUS_OK; } if (401 == http_status) { status_code = EXO_STATUS_NOAUTH; } return vlen; }
void write_to_logfile(struct sockaddr_in cli_addr, char *path_to_file_relativ, char buffer[], int read_count_bytes) { FILE *f = fopen("logfile.txt", "a"); if (f == NULL) { printf("Error opening file!\n"); exit(1); } time_t t; struct tm *ts; t = time(NULL); ts = localtime(&t); char *p; struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&cli_addr; struct in_addr ipAddr = pV4Addr->sin_addr; char str[INET_ADDRSTRLEN]; inet_ntop( AF_INET, &ipAddr, str, INET_ADDRSTRLEN ); p = strtok(buffer, " "); //printf("%s - - [%i/%s/%i:%02i:%02i:%02i +0200] \"%s %s\" %i %s %i\n", str, ts->tm_mday, get_month(ts->tm_mon), ts->tm_year + 1900, ts->tm_hour, ts->tm_min, ts->tm_sec, p, path_to_file_relativ, http_status_list[get_http_status()].code, http_status_list[get_http_status()].text, read_count_bytes); fprintf(f, "%s - - [%i/%s/%i:%02i:%02i:%02i +0200] \"%s %s\" %i %s %i\n", str, ts->tm_mday, get_month(ts->tm_mon), ts->tm_year + 1900, ts->tm_hour, ts->tm_min, ts->tm_sec, p, path_to_file_relativ, http_status_list[get_http_status()].code, http_status_list[get_http_status()].text, read_count_bytes); }
char* create_HTTP_response_header(const char *filename, char buffer[]) { //printf("................\n"); char* response_header = (char*) malloc(BUFFER_SIZE); int range_end; int range_start = 0; int content_length; char *ptr; char *lineRange = NULL; //char *lineModified = NULL; if(response_header == NULL) { error("Error allocating response_header"); } char status_text[100] = "HTTP/1.1 %i Partial Content\r\n"; char date_text[100] = "DATUM Funktion einbauen\r\n"; char server_text[100] = "Server: TinyWeb (Build Jun 12 2014)\r\n"; char accept_range_text[100] = "Accept-Ranges: bytes\r\n"; char last_modified_text[100] = "Last-Modified: Thu, 12 Jun 2014\r\n"; char content_type_text[100] = "Content-Type: text/html\r\n"; char content_length_text[100] = "Content-Length: 1004\r\n"; char content_range_text[100] = "Content-Range: bytes 6764-7767/7768\r\n"; char connection_text[100] = "Connection: Close\r\n\r\n"; //file length calculating struct stat buf; if(stat(filename, &buf) != 0) { printf("Error in file length calculating.\n"); } //set_http_status(HTTP_STATUS_PARTIAL_CONTENT); time_t t; struct tm *ts; t = time(NULL); ts = localtime(&t); struct stat file_Info; sprintf(status_text, "HTTP/1.1 %i %s\r\n", http_status_list[get_http_status()].code, http_status_list[get_http_status()].text ); //TODO: status dynamisch uebergeben sprintf(date_text, "Date: %s, %i %s %i %02i:%02i:%02i GMT\r\n", get_weekday(ts->tm_wday), ts->tm_mday, get_month(ts->tm_mon), ts->tm_year + 1900, ts->tm_hour, ts->tm_min, ts->tm_sec); //TODO: Reutemann fragen ob das Format so passt //sprintf(server_text, "Server: TinyWeb (Build Jun 12 2014)", ); //TODO: Buildzeit dynamisch einfuegen ptr = strtok(NULL, "\n"); while (ptr != NULL) { // extract line with the range if existing if (strncmp(ptr,"Range",5) == 0) { lineRange = ptr; } else if ((strncmp(ptr,"If-Modified-Since",strlen("If-Modified-Since"))) == 0) { //lineModified = ptr; } ptr = strtok(NULL, "\n"); } if (lineRange != NULL) { char *range = malloc(strlen(lineRange)+1); if (range == NULL) { perror("Malloc():"); } strtok(lineRange,"="); range = strtok(NULL,"="); // after "=" range_start = atoi(strtok(range,"-")); range_end = atoi(strtok(NULL,"-")); //printf("Start: %i End: %i", range_start, range_end); } int check; check = stat(filename, &file_Info); if (check < 0) { printf("NOT FOUND ACCESS CHECK "); set_http_status(HTTP_STATUS_NOT_FOUND); } // get last modified char* last_modified = malloc(32); struct tm * timeinfo; timeinfo = localtime(&file_Info.st_mtim.tv_sec); strftime (last_modified,32,"%a, %d %b %Y %H:%M:%S %Z",timeinfo); sprintf(last_modified_text, "Last-Modified: %s\n", last_modified); //TODO: Dateidatum einfuegen sprintf(content_type_text, "Content-Type: %s\r\n", get_http_content_type_str(get_http_content_type(filename))); range_end = file_Info.st_size; if(range_end < 0) { error("Error with range"); } else { content_length = range_end - range_start; } sprintf(content_length_text, "Content-Length: %i\r\n", content_length); sprintf(content_range_text, "Content-Range: bytes %i-%i/%i\n", range_start, range_end, content_length ); //TODO: Frage was das ist und wie dynamisch abgefragt wird strcat(response_header, status_text); strcat(response_header, date_text); strcat(response_header, server_text); strcat(response_header, accept_range_text); strcat(response_header, last_modified_text); strcat(response_header, content_type_text); strcat(response_header, content_length_text); strcat(response_header, content_range_text); strcat(response_header, connection_text); printf("\n------------Response-----------------\n%s------------Response-----------------\n", response_header); return response_header; }
/***************************************************************************** * * Exosite_Activate * * \param None * * \return 1 - activation success * 0 - activation failure * * \brief Called after Init has been run in the past, but maybe comms were * down and we have to keep trying * *****************************************************************************/ int Exosite_Activate(void) { int length; char strLen[5]; char *cmp_ss = "Content-Length: 40"; char *cmp = cmp_ss; int newcik = 0; int http_status = 0; if (!exosite_initialized) { status_code = EXO_STATUS_INIT; return newcik; } update_m2ip(); //check our IP api to see if the old IP is advertising a new one long sock = connect_to_exosite(); if (sock < 0) { status_code = EXO_STATUS_BAD_TCP; return 0; } // Get activation Serial Number length = strlen(exosite_provision_info); sprintf(strLen, "%d", length); //make a string for length sendLine(sock, POSTDATA_LINE, "/provision/activate"); sendLine(sock, HOST_LINE, NULL); sendLine(sock, USER_AGENT_LINE, NULL); sendLine(sock, CONNECTION_LINE, NULL); sendLine(sock, CONTENT_LINE, NULL); sendLine(sock, LENGTH_LINE, strLen); exoHAL_SocketSend(sock, exosite_provision_info, length); http_status = get_http_status(sock); if (200 == http_status) { char strBuf[RX_SIZE]; unsigned char strLen, len; unsigned char cik_len_valid = 0; char *p; unsigned char crlf = 0; unsigned char ciklen = 0; char NCIK[CIK_LENGTH + 1]; do { strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); len = strLen; p = strBuf; // Find 4 consecutive \r or \n - should be: \r\n\r\n while (0 < len && 4 > crlf) { if ('\r' == *p || '\n' == *p) { ++crlf; } else { crlf = 0; if (*cmp == *p) { // check the cik length from http response cmp++; if (cmp > cmp_ss + strlen(cmp_ss) - 1) cik_len_valid = 1; } else cmp = cmp_ss; } ++p; --len; } // The body is the cik if (0 < len && 4 == crlf && CIK_LENGTH > ciklen) { // TODO, be more robust - match Content-Length header value to CIK_LENGTH unsigned char need, part; if (!(cik_len_valid == 1)) // cik length != 40 { status_code = EXO_STATUS_CONFLICT; exoHAL_SocketClose(sock); return newcik; } need = CIK_LENGTH - ciklen; part = need < len ? need : len; strncpy(NCIK + ciklen, p, part); ciklen += part; } } while (RX_SIZE == strLen); if (CIK_LENGTH == ciklen) { NCIK[40] = 0; Exosite_SetCIK(NCIK); newcik = 1; } } exoHAL_SocketClose(sock); if (200 == http_status) status_code = EXO_STATUS_OK; if (404 == http_status) status_code = EXO_STATUS_BAD_SN; if (409 == http_status || 408 == http_status) { status_code = EXO_STATUS_CONFLICT; } return newcik; }
int process_description(request *req, node_t *me) { node_t *node, *next; if(req->method != M_GET) return R_METHOD_NA; if(!me->parent) return R_BAD_REQUEST; xadd_stag_attr(req, TAG_ResourceDescription, "version=\"%s\" xmlns=\"urn:psialliance-org\"", me->version); node = (me->parent->type != NODE_SERVICE) ? me->parent : me->parent->first_child; while(node) { next = (me->parent->type != NODE_SERVICE) ? NULL : node->next; xadd_elem(req, TAG_name, &node->name[1]); xadd_elem(req, TAG_version, node->version); if(node->type == NODE_SERVICE) xadd_elem(req, TAG_type, "service"); else xadd_elem(req, TAG_type, "resource"); if(node->methods & M_GET) { xadd_stag(req, TAG_get); if(node->get_query_str) xadd_elem(req, TAG_queryStringParameterList, node->get_query_str); if(node->get_inbound_str) xadd_elem(req, TAG_inboundXML, node->get_inbound_str); if(node->get_function_str) xadd_elem(req, TAG_function, node->get_function_str); if(node->get_result_str) xadd_elem(req, TAG_returnResult, node->get_result_str); if(node->get_notes_str) xadd_elem(req, TAG_notes, node->get_notes_str); xadd_etag(req, TAG_get); } if(node->methods & M_PUT) { xadd_stag(req, TAG_put); if(node->put_query_str) xadd_elem(req, TAG_queryStringParameterList, node->put_query_str); if(node->put_inbound_str) xadd_elem(req, TAG_inboundXML, node->put_inbound_str); if(node->put_function_str) xadd_elem(req, TAG_function, node->put_function_str); if(node->put_result_str) xadd_elem(req, TAG_returnResult, node->put_result_str); if(node->put_notes_str) xadd_elem(req, TAG_notes, node->put_notes_str); xadd_etag(req, TAG_put); } if(node->methods & M_POST) { xadd_stag(req, TAG_post); if(node->post_query_str) xadd_elem(req, TAG_queryStringParameterList, node->post_query_str); if(node->post_inbound_str) xadd_elem(req, TAG_inboundXML, node->post_inbound_str); if(node->post_function_str) xadd_elem(req, TAG_function, node->post_function_str); if(node->post_result_str) xadd_elem(req, TAG_returnResult, node->post_result_str); if(node->post_notes_str) xadd_elem(req, TAG_notes, node->post_notes_str); xadd_etag(req, TAG_post); } if(node->methods & M_DELETE) { xadd_stag(req, TAG_delete); if(node->del_query_str) xadd_elem(req, TAG_queryStringParameterList, node->del_query_str); if(node->del_inbound_str) xadd_elem(req, TAG_inboundXML, node->del_inbound_str); if(node->del_function_str) xadd_elem(req, TAG_function, node->del_function_str); if(node->del_result_str) xadd_elem(req, TAG_returnResult, node->del_result_str); if(node->del_notes_str) xadd_elem(req, TAG_notes, node->del_notes_str); xadd_etag(req, TAG_delete); } node = next; } xadd_etag(req, TAG_ResourceDescription); req->status_code = STATUS_OK; return (get_http_status(req)); }
/***************************************************************************** * * Exosite_Read * * \param palias - string, name of the datasource alias to read from * pbuf - read buffer to put the read response into * buflen - size of the input buffer * * \return number of bytes read * * \brief Reads data from Exosite cloud * *****************************************************************************/ int Exosite_Read(char * palias, char * pbuf, unsigned char buflen) { int success = 0; int http_status = 0; char bufCIK[41]; unsigned char strLen, len, vlen; char *p, *pcheck; if (!exosite_initialized) { status_code = EXO_STATUS_INIT; return success; } if (!Exosite_GetCIK(bufCIK)) { return success; } long sock = connect_to_exosite(); if (sock < 0) { status_code = EXO_STATUS_BAD_TCP; return 0; } // This is an example read GET // s.send('GET /onep:v1/stack/alias?temp HTTP/1.1\r\n') // s.send('Host: m2.exosite.com\r\n') // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n') // s.send('Accept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n') sendLine(sock, GETDATA_LINE, palias); sendLine(sock, HOST_LINE, NULL); sendLine(sock, USER_AGENT_LINE, NULL); sendLine(sock, CONNECTION_LINE, NULL); sendLine(sock, CIK_LINE, bufCIK); sendLine(sock, ACCEPT_LINE, "\r\n"); pcheck = palias; vlen = 0; http_status = get_http_status(sock); if (200 == http_status) { char strBuf[RX_SIZE]; unsigned char crlf = 0; do { strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); len = strLen; p = strBuf; // Find 4 consecutive \r or \n - should be: \r\n\r\n while (0 < len && 4 > crlf) { if ('\r' == *p || '\n' == *p) { ++crlf; } else { crlf = 0; } ++p; --len; } // The body is "<key>=<value>" if (0 < len && 4 == crlf && buflen > vlen) { // Move past "<key>" while (0 < len && 0 != *pcheck) { if (*pcheck == *p) { ++pcheck; } else { pcheck = palias; } ++p; --len; } // Match '=', we should now have '<key>=' if (0 < len && 0 == *pcheck && '=' == *p) { ++p; --len; } // read in the rest of the body as the value while (0 < len && buflen > vlen) { pbuf[vlen++] = *p++; --len; } } } while (RX_SIZE == strLen); } exoHAL_SocketClose(sock); if (200 == http_status) { status_code = EXO_STATUS_OK; } if (204 == http_status) { status_code = EXO_STATUS_OK; } if (401 == http_status) { status_code = EXO_STATUS_NOAUTH; } return vlen; }