testclient.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* quick and dirty test for chunked encoding */
  2. #ifdef WIN32
  3. #include <WinSock2.h>
  4. #else
  5. #include <unistd.h>
  6. #include <arpa/inet.h>
  7. #include <netdb.h>
  8. #include <termios.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/ioctl.h>
  12. #include <netinet/in.h>
  13. #include <netinet/tcp.h>
  14. #include <netinet/ip.h>
  15. typedef int SOCKET;
  16. typedef struct sockaddr SOCKADDR;
  17. typedef struct hostent HOSTENT;
  18. const int INVALID_SOCKET = -1;
  19. const int SD_SEND = SHUT_WR;
  20. const int SD_BOTH = SHUT_RDWR;
  21. #define closesocket(x) close(x)
  22. #define Sleep(x) usleep((x)*1000)
  23. #define ioctlsocket(a,b,c) ioctl(a,b,c)
  24. #endif
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <time.h>
  28. char * HOST = "127.0.0.1";
  29. unsigned short PORT = 8080;
  30. const char * RESOURCE = "/resource_script_demo.lua/r1.txt";
  31. const char * METHOD = "PUT";
  32. unsigned postSize = 987;
  33. unsigned extraHeadSize = 0;
  34. unsigned queryStringSize = 0;
  35. int keep_alive = 0;
  36. int chunked = 1;
  37. int sockvprintf(SOCKET soc, const char * fmt, va_list vl) {
  38. char buf[1024*8];
  39. #ifdef WIN32
  40. int len = vsprintf_s(buf, sizeof(buf), fmt, vl);
  41. #else
  42. int len = vsprintf(buf, fmt, vl);
  43. #endif
  44. int ret = send(soc, buf, len, 0);
  45. return ret;
  46. }
  47. int sockprintf(SOCKET soc, const char * fmt, ...) {
  48. int ret = -1;
  49. va_list vl;
  50. va_start(vl, fmt);
  51. ret = sockvprintf(soc, fmt, vl);
  52. va_end(vl);
  53. return ret;
  54. }
  55. static struct sockaddr_in target = {0};
  56. int TestClient(unsigned clientNo) {
  57. SOCKET soc;
  58. time_t lastData;
  59. size_t totalData = 0;
  60. size_t bodyData = 0;
  61. int isBody = 0;
  62. int timeOut = 10;
  63. unsigned long i;
  64. // TCP
  65. soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  66. if (soc==INVALID_SOCKET) {
  67. printf("\r\nClient %u: cannot create socket\a\r\n", (int)clientNo);
  68. return 3;
  69. }
  70. // comment in to disable Nagle:
  71. {int disable_Nagle = 1; setsockopt(soc, IPPROTO_TCP, TCP_NODELAY, (char *) &disable_Nagle, sizeof(disable_Nagle));}
  72. if (connect(soc, (SOCKADDR*)&target, sizeof(target))) {
  73. printf("\r\nClient %u: cannot connect to server %s:%u\a\r\n", (int)clientNo, HOST, PORT);
  74. return 4;
  75. }
  76. sockprintf(soc, "%s %s", METHOD, RESOURCE);
  77. if (queryStringSize>0) {
  78. sockprintf(soc, "?", METHOD, RESOURCE);
  79. for (i=0;i<(queryStringSize/10);i++) {sockprintf(soc, "1234567890");}
  80. for (i=0;i<(queryStringSize%10);i++) {sockprintf(soc, "_");}
  81. }
  82. sockprintf(soc, " HTTP/1.1\r\nHost: %s\r\n", HOST);
  83. if (keep_alive) {
  84. sockprintf(soc, "Connection: Keep-Alive\r\n");
  85. } else {
  86. sockprintf(soc, "Connection: Close\r\n");
  87. }
  88. for (i=0;i<(extraHeadSize/25);i++) {
  89. sockprintf(soc, "Comment%04u: 1234567890\r\n", i % 10000);
  90. }
  91. if (!strcmp(METHOD, "GET")) {
  92. sockprintf(soc, "\r\n");
  93. } else if (chunked) {
  94. unsigned remaining_postSize = postSize;
  95. sockprintf(soc, "Transfer-Encoding: chunked\r\n\r\n", postSize);
  96. while (remaining_postSize > 0) {
  97. unsigned chunk = rand()%200 + 1;
  98. if (chunk>remaining_postSize) chunk = remaining_postSize;
  99. sockprintf(soc, "%x\r\n", chunk);
  100. for (i=0;i<chunk;i++) {sockprintf(soc, "_");}
  101. sockprintf(soc, "\r\n");
  102. remaining_postSize -= chunk;
  103. }
  104. sockprintf(soc, "0\r\n\r\n", postSize%10);
  105. } else {
  106. sockprintf(soc, "Content-Length: %u\r\n\r\n", postSize);
  107. for (i=0;i<postSize/10;i++) {sockprintf(soc, "1234567890");}
  108. for (i=0;i<postSize%10;i++) {sockprintf(soc, ".");}
  109. timeOut += postSize/10000;
  110. }
  111. if (!keep_alive) {
  112. shutdown(soc, SD_SEND);
  113. } else {
  114. timeOut = 2;
  115. }
  116. // wait for response from the server
  117. bodyData = totalData = 0;
  118. isBody = 0;
  119. lastData = time(0);
  120. for (;;) {
  121. char buf[20480];
  122. int chunkSize = 0;
  123. unsigned long dataReady = 0;
  124. Sleep(1);
  125. if (ioctlsocket(soc, FIONREAD, &dataReady) < 0) break;
  126. if (dataReady) {
  127. chunkSize = recv(soc, buf+totalData, sizeof(buf)-totalData, 0);
  128. if (chunkSize<0) {
  129. printf("Error: recv failed for client %i\r\n", (int)clientNo);
  130. break;
  131. } else if (!isBody) {
  132. char * headEnd = strstr(buf,"\xD\xA\xD\xA");
  133. if (headEnd) {
  134. headEnd+=4;
  135. chunkSize -= ((int)headEnd - (int)buf);
  136. if (chunkSize>0) {
  137. //fwrite(headEnd,1,got,STORE);
  138. bodyData += chunkSize;
  139. }
  140. isBody=1;
  141. }
  142. } else {
  143. //fwrite(buf,1,got,STORE);
  144. bodyData += chunkSize;
  145. }
  146. lastData = time(0);
  147. totalData += chunkSize;
  148. } else {
  149. time_t current = time(0);
  150. if (difftime(current, lastData) > timeOut) {
  151. break;
  152. }
  153. Sleep(10);
  154. }
  155. }
  156. shutdown(soc, SD_BOTH);
  157. closesocket(soc);
  158. return 0;
  159. }
  160. int main(int argc, char * argv[]) {
  161. HOSTENT * lpHost = 0;
  162. #ifdef WIN32
  163. WSADATA wsaData = {0};
  164. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
  165. printf("\r\nCannot init WinSock\a\r\n");
  166. return 1;
  167. }
  168. #endif
  169. srand((unsigned int)time(NULL));
  170. lpHost = gethostbyname(HOST);
  171. if (lpHost == NULL) {
  172. printf("\r\nCannot find host %s\a\r\n",HOST);
  173. return 2;
  174. }
  175. target.sin_family = AF_INET;
  176. #ifdef WIN32
  177. target.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
  178. #else
  179. target.sin_addr.s_addr = htonl(0x7F000001);
  180. #endif
  181. target.sin_port = htons(PORT);
  182. printf("\r\nConnect to %s\r\n", inet_ntoa(target.sin_addr));
  183. TestClient(1);
  184. #ifdef WIN32
  185. WSACleanup();
  186. #endif
  187. return 0;
  188. }