client_thread.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "stdafx.h"
  2. #include "classdef.h"
  3. #include "winsock2.h"
  4. #include "constdef.h"
  5. #include <sys/stat.h>
  6. #include <map>
  7. using namespace std;
  8. void sendHead(SOCKET s, int state, char* fileExtension, char* version);
  9. bool sendContent(SOCKET s, char* filePath);
  10. map<string, string> map_param;
  11. char ascii_string[10000];
  12. char *char_to_ascii(char ch){
  13. char *ascii_str;
  14. ascii_string[0] = 0;
  15. ascii_str = ascii_string;
  16. if(isgraph(ch)){
  17. *ascii_str++ = ch;
  18. }else if(ch == ' '){
  19. *ascii_str++ =ch;
  20. }else if(ch == '\n' || ch == '\r'){
  21. *ascii_str++ =ch;
  22. }else{
  23. *ascii_str++ ='.';
  24. }
  25. *ascii_str = 0;
  26. return ascii_string;
  27. }
  28. void parse_interface(){
  29. string username, inter_face, json_data, version;
  30. map<string, string>::iterator it_para = map_param.begin();
  31. for(; it_para != map_param.end(); ++it_para){
  32. if(it_para->first == PARAM_NAME_USERNAME){
  33. username = it_para->second;
  34. }else if(it_para->first == PARAM_NAME_INTERFACE){
  35. inter_face = it_para->second;
  36. }else if(it_para->first == PARAM_NAME_DATA){
  37. json_data = it_para->second;
  38. }else if(it_para->first == PARAM_NAME_VERSION){
  39. version = it_para->second;
  40. }
  41. }
  42. //
  43. if(version == "1.0"){
  44. if(inter_face == INTERFACE_NAME_SETLIMITVALUE){
  45. }else if(inter_face == INTERFACE_NAME_CALLCARD){
  46. }else if(inter_face == INTERFACE_NAME_CLEARCARD){
  47. }else if(inter_face == INTERFACE_NAME_EDITAREA){
  48. }else if(inter_face == INTERFACE_NAME_EDITSTATION){
  49. }else if(inter_face == INTERFACE_NAME_EDITPATH){
  50. }else if(inter_face == INTERFACE_NAME_EDITCARD){
  51. }else if(inter_face == INTERFACE_NAME_EDITSTAFFER){
  52. }else if(inter_face == INTERFACE_NAME_EDITVEHICLE){
  53. }
  54. }
  55. }
  56. void parse_param_data(char content[], int number){
  57. //char temp[1000];
  58. int i,j = 0;
  59. bool bkey = true;
  60. string para_key = "", para_value = "";
  61. for(i = 0; i < number; i++){
  62. if(content[i] == '&'){
  63. bkey = true;
  64. para_value += '\0';
  65. i++;
  66. map_param.insert(make_pair(para_key, para_value));
  67. para_key = "";
  68. para_value = "";
  69. j = 0;
  70. }
  71. if(content[i] == '='){
  72. bkey = false;
  73. para_key += '\0';
  74. i++;
  75. j = 0;
  76. }
  77. if(bkey){
  78. para_key += content[i];
  79. }else{
  80. para_value += content[i];
  81. }
  82. j++;
  83. }
  84. para_value += '\0';
  85. map_param.insert(make_pair(para_key, para_value));
  86. }
  87. void parse_server_data(char content[], size_t number){
  88. char temp[1024];
  89. char str1[1024];
  90. char str2[1024];
  91. char str3[1024];
  92. char paralen[20];
  93. size_t i, j, k = 0, m = 0;
  94. char entity_content[1024];
  95. for(i = 0; i < number; i++){
  96. if(content[i] != '\n'){
  97. k++;
  98. continue;
  99. }
  100. for(j = 0; j < k; j++)
  101. temp[j] = content[j + i - k];
  102. temp[j] = '\0';
  103. if(strstr(temp, "GET") || strstr(temp, "POST")){ // 请求行为
  104. sscanf_s(temp, "%s %s %s", str1, str2, str3); // 命令,路径,协议版本
  105. }
  106. //if(strstr(temp, "Accept:")){ // 数据文件包括(Accept:)
  107. //}
  108. //if(strstr(temp, "Referer")){ // 转移地址(Referer)
  109. //}
  110. //if(strstr(temp, "Accept-Language")){ // 使用的语言
  111. //}
  112. //if(strstr(temp, "Accept-Encoding")){ // 编码方式
  113. //}
  114. //if(strstr(temp, "If-Modified-Since")){ // 上次修改时间
  115. //}
  116. //if(strstr(temp, "If-Node-Match")){ //
  117. //}
  118. //if(strstr(temp, "User-Agent")){ // 用户浏览器信息
  119. //}
  120. //if(strstr(temp, "Host")){ // 访问的主机
  121. //}
  122. //if(strstr(temp, "Connection")){ // 连接状态
  123. //}
  124. //if(strstr(temp, "Cookie")){ // cookie
  125. //}
  126. //if(strstr(temp, "Cache-Control:")){ //
  127. //}
  128. //if(strstr(temp, "Content-Type:")){ // 实体数据格式
  129. //
  130. //}
  131. if(strstr(temp, "Content-Length:")){ // 数据长度
  132. //sscanf(temp, "%s%d", temp, paralen);
  133. m = 0;
  134. for(j = 15; j < strlen(temp); j++){
  135. if(temp[j] != ' ' && temp[j] != '\n'){
  136. paralen[m] = temp[j];
  137. m++;
  138. }
  139. }
  140. paralen[m] = '\0';
  141. m = atoi(paralen);
  142. }
  143. // 实体数据
  144. if((content[i] == '\n') && content[i + 1] == '\r' && content[i + 2] == '\n'){
  145. if(i + 3 == strlen(content)){ // 无数据
  146. break;
  147. }
  148. //for (j = 0; j < strlen(content) - i - 3; j++){
  149. for(j = 0; j < m; j++){
  150. entity_content[j] = content[i + 3 + j];
  151. }
  152. entity_content[j] = '\0';
  153. string estr = entity_content;
  154. // 解析参数
  155. parse_param_data(entity_content, m);
  156. break;
  157. }
  158. k = 0;
  159. }
  160. }
  161. DWORD WINAPI clientThread(LPVOID lparam)
  162. {
  163. PARA* para = (PARA*)lparam;
  164. char buf[MAXSIZE], filePath[200], cmd[50], version[50], fileExtension[50]={'\0'};
  165. int recvCount;
  166. filePath[0]='.';
  167. //int aa, bb, cc = 0;
  168. //char temp1[200], temp2[200] = {0};
  169. recvCount = recv(para->s, buf, MAXSIZE, 0);
  170. sscanf_s(buf, "%s %s %s", cmd, filePath + 1, version);
  171. parse_server_data(buf, recvCount);
  172. closesocket(para->s);
  173. delete para;
  174. return 0;
  175. }