bool BasicHttpRequest::send_file( const char *url ) { // open int src = open( url, O_RDONLY ); if ( src < 0 ) { if ( not *url ) return send_file( "index.html" ); return false; } // stat struct stat stat_buf; if ( fstat( src, &stat_buf ) ) { close( src ); return false; } // directory ? if ( S_ISDIR( stat_buf.st_mode ) ) { std::string nrl = url; nrl += "/index.html"; return send_file( nrl.c_str() ); } // send_head( url ); send( src, 0, stat_buf.st_size ); return true; }
/* 处理静态内容 */ void deal_static(int client_sock,char *path,int file_size){ //send_head(client_sock,path); //FILE *file=fopen(path,"r"); char *buf; char file[file_size]; //对buf进行内存分配 buf = (char *)malloc(file_size); //打开文件 int fd = open(path,O_RDONLY); if(fd==-1){ not_found(client_sock,path); }else{ //发送头部 send_head(client_sock,path); //获取内容 get_content(buf,fd,file_size); //给客户端发送文件内容 send(client_sock,buf,file_size,0); } //释放内容,这很重要 free(buf); close(fd); return; }
void send_connect_auth(char *cmd) { char *total_packg=NULL; char *userid; char *mac_data; char *ip_data; char ip_temp[40]; char *cuniqid; char uid_tmp[60]; char type[10]; unsigned char authbody_size; memset(uid_tmp,0,sizeof(char)*60); memset(type,0,sizeof(char)*10); memset(ip_temp,0,sizeof(char)*40); cuniqid= (char*)calloc(10,sizeof(char)); mac_data=(char*)calloc(30,sizeof(char)); ip_data=(char*)calloc(25,sizeof(char)); userid=(char*)calloc(60,sizeof(char)); if(!get_ip(ETH,ip_temp)) { DEBUG_printf("get ip error!\n"); } sprintf(ip_data,"IP=%s,",ip_temp); strcpy( userid,"UID="); #ifdef BB_BLACK strcat( userid,read_conf_by_type(bb_black_conf_dir,"api-uid",uid_tmp)); #endif // BB_BLACK #ifdef OPENWRT strcat( userid,read_luci_conf("user_id")); #endif // OPENWRT strcat( userid,","); sprintf(type,"TYPE=%d",MINERTYPE); authbody_size=strlen(gen_cuniqid(deal_package_cmd.pkg_nonce))+strlen(get_mac(mac_data,ETH))+strlen(ip_data)+strlen(userid)+strlen(type); send_head(cmd,authbody_size); total_packg=(char*)calloc((authbody_size+4),sizeof(char)); strcpy(total_packg,deal_package_cmd.pkg_nonce); strcat(total_packg,mac_data); strcat(total_packg,ip_data); strcat(total_packg,userid); strcat(total_packg,type); if ((sendbytes = senddata(ssl,sockfd,total_packg,authbody_size)) == -1) { perror("send"); //exit(1); } DEBUG_printf("send pakge str :%s\n",total_packg); free(mac_data); free(ip_data); free(userid); free(cuniqid); if(total_packg) free(total_packg); }
//LED with Ic show by panel mode function //r,g,b,w is RGBW data of 30 LEDs void RGBW_Ic_Panel_Show(uint16_t r, uint16_t g, uint16_t b, uint16_t w,uint16_t num) { uint8_t rgbw[4]={w,r,g,b}; send_head(); for(uint16_t i=0;i<num;i++) { send_RGBW_IC(rgbw); } delayMicroseconds(250); }
int Http::set_host(const char *host, int port) { if (strchr(host, ':')) { /* ipv6 */ #ifdef HAVE_SSL if ((useSSL && port != 443) || (!useSSL && port != 80)) { #else if (port != 80) { #endif snprintf(buf, sizeof(buf), "[%s]:%d", host, port); } else { snprintf(buf, sizeof(buf), "[%s]", host); } snprintf(buf, sizeof(buf), "[%s]:%d", host, port); } else { #ifdef HAVE_SSL if ((useSSL && port != 443) || (!useSSL && port != 80)) { #else if (port != 80) { #endif snprintf(buf, sizeof(buf), "%s:%d", host, port); } else { snprintf(buf, sizeof(buf), "%s", host); } } return request.set_attr("Host", buf); }; int Http::send_head() { HeadDataNode *it; int ret; for (it = request.head; it != NULL; it = it->next) { snprintf(buf, sizeof(buf), "%s: %s\r\n", it->attrName, it->attrValue); if ((ret=conn->write(buf, timeout)) < 0) return ret; log("%s: %s\r\n", it->attrName, it->attrValue); } snprintf(buf, sizeof(buf), "\r\n"); if ((ret=conn->write(buf, timeout)) < 0) return ret; log("\r\n"); return 0; }; int Http::head(const char *url) { int ret; snprintf(buf, sizeof(buf), "HEAD %s HTTP/%s\r\n", url, HTTP_VERSION); if ((ret=conn->write(buf, timeout)) < 0) return ret; log("HEAD %s HTTP/%s\r\n", url, HTTP_VERSION); RETURN_IF_FAILED(send_head()); return 0; };
void RGBW_Ic_Shift_Show(uint8_t p[256][4]) { send_head(); for(uint16_t i=0;i<256;i++) { send_RGBW_IC(p[i]); } delayMicroseconds(250); }
int Http::get(const char *url) { int ret; snprintf(buf, sizeof(buf), "GET %s HTTP/%s\r\n", url, HTTP_VERSION); if ((ret=conn->write(buf, timeout)) < 0) return ret; log("GET %s HTTP/%s\r\n", url, HTTP_VERSION); RETURN_IF_FAILED(send_head()); conn->set_tos(); return 0; };
//////////////////////////////////////////////////// ////////////Send CONNECT_CC///////////////////////// int send_connect_cc(char *cmd) { int revbody_size=strlen(buf); DEBUG_printf("02revbody_size=%d\n",revbody_size); send_head(cmd,revbody_size); if ((sendbytes = senddata(ssl,sockfd,buf, revbody_size)) == -1) { perror("send"); exit(1); } DEBUG_printf("send what I rev:\n%s\n",buf); return 1; }
// After reboot is complete, client send SET_CONFIG_RES void send_setconfig_result(char *cmd,char *conf_stat) { char *cuniqid; char *buf_data; int body_len; cuniqid= (char*)calloc(10,sizeof(char)); buf_data=(char*)calloc(60,sizeof(char)); strcpy(buf_data,gen_cuniqid(deal_package_cmd.pkg_nonce)); strcat(buf_data,"ID="); strcat(buf_data,rev_ID); strcat(buf_data,",result="); strcat(buf_data,conf_stat); body_len=strlen(buf_data); send_head(cmd, body_len); if((sendbytes = senddata(ssl,sockfd,buf_data, body_len)) == -1) { perror("send"); //exit(1); } DEBUG_printf("send cgminer state :%s\n",buf_data); //free(cuniqid); free(buf_data); }
/////////////////////03--30--03--30//////////////////////////// ///////////////////get_status_res////////////////////////////// ////////////get cgminer state&& send//////////////////// int send_getstatus_res(char *cmd_server_code) { char *api_len=NULL; char *buf_data=NULL; char *cuniqid; int len,i; short int port; int statebody_size=0; char ipaddr[40]; char *cmd_server; char *s4_dataformat_dir; char *api_command; for(i=0;i<COMMAND_SUM;i++) { if(strcmp(cmd_server_code,server_command_code[i])==0) { len = strlen(dataformat_conf_dir[i]) + 1; s4_dataformat_dir =(char *)malloc(len * sizeof(char)); memset(s4_dataformat_dir,0,len); strcpy(s4_dataformat_dir,dataformat_conf_dir[i]); len = strlen(server_command[i]) + 1; api_command = (char *)malloc(len * sizeof(char)); memset(api_command,0,len); strcpy(api_command,server_command[i]); len = strlen(command_code[i]) + 1; cmd_server = (char *)malloc(len * sizeof(char)); memset(cmd_server,0,len); strcpy(cmd_server,command_code[i]); break; } } buf_data=(char*)calloc(4096,sizeof(char)); //cuniqid= (char*)calloc(10,sizeof(char)); len=strlen(gen_cuniqid(deal_package_cmd.pkg_nonce)); //Get_Port //Get_IP #ifdef OPENWRT port=4028; #endif // OPENWRT #ifdef BB_BLACK char port_str[10]; port=atoi(read_bb_black_switch(bb_black_conf_dir,"port_local",port_str)); #endif // BB_BLACK if(!get_ip(ETH,ipaddr)) { DEBUG_printf("get IP error\n"); } #ifdef PC_IP port=4028; strcpy(ipaddr,ip_pc); #endif api_len=callapi(api_command, ipaddr,port); if(strlen(api_len) == 0) { DEBUG_printf("nothing returned form callapi\n\n\n "); return 0; } //DEBUG_printf("result of callapi:%s\n ",api_len); statebody_size=strlen(data_format(s4_dataformat_dir,buf_data,api_len)); DEBUG_printf("state_len=%d\n",(statebody_size+len)); send_head(cmd_server,(statebody_size+len)); if ((sendbytes = senddata(ssl,sockfd,deal_package_cmd.pkg_nonce, len)) ==-1) { perror("send"); } if ( (sendbytes =senddata(ssl,sockfd,buf_data, (statebody_size))) ==-1) { perror("send"); } DEBUG_printf("sendbytes=%d\n",sendbytes); DEBUG_printf("send pakge str :%s\n",buf_data); if(cmd_server) free(cmd_server); if(s4_dataformat_dir) free(s4_dataformat_dir); if(api_command) free(api_command); if(api_len) free(api_len); free(buf_data); //free(cuniqid); return 1; }