testclient.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = 1024;
  33. unsigned extraHeadSize = 0;
  34. int keep_alive = 0;
  35. int chunked = 0;
  36. int sockvprintf(SOCKET soc, const char * fmt, va_list vl) {
  37. char buf[1024*8];
  38. #ifdef WIN32
  39. int len = vsprintf_s(buf, sizeof(buf), fmt, vl);
  40. #else
  41. int len = vsprintf(buf, fmt, vl);
  42. #endif
  43. int ret = send(soc, buf, len, 0);
  44. return ret;
  45. }
  46. int sockprintf(SOCKET soc, const char * fmt, ...) {
  47. int ret = -1;
  48. va_list vl;
  49. va_start(vl, fmt);
  50. ret = sockvprintf(soc, fmt, vl);
  51. va_end(vl);
  52. return ret;
  53. }
  54. static struct sockaddr_in target = {0};
  55. int TestClient(unsigned clientNo) {
  56. SOCKET soc;
  57. time_t lastData;
  58. size_t totalData = 0;
  59. size_t bodyData = 0;
  60. int isBody = 0;
  61. int isTest = (clientNo == 0);
  62. int cpu = ((int)clientNo) % 100;
  63. int timeOut = 10;
  64. unsigned long i, j;
  65. // TCP
  66. soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  67. if (soc==INVALID_SOCKET) {
  68. printf("\r\nClient %u: cannot create socket\a\r\n", (int)clientNo);
  69. return 3;
  70. }
  71. // comment in to disable Nagle:
  72. {int disable_Nagle = 1; setsockopt(soc, IPPROTO_TCP, TCP_NODELAY, (char *) &disable_Nagle, sizeof(disable_Nagle));}
  73. if (connect(soc, (SOCKADDR*)&target, sizeof(target))) {
  74. printf("\r\nClient %u: cannot connect to server %s:%u\a\r\n", (int)clientNo, HOST, PORT);
  75. return 4;
  76. }
  77. sockprintf(soc, "%s %s", METHOD, RESOURCE);
  78. // could send query string here
  79. sockprintf(soc, " HTTP/1.1\r\nHost: %s\r\n", HOST);
  80. if (keep_alive) {
  81. sockprintf(soc, "Connection: Keep-Alive\r\n");
  82. } else {
  83. sockprintf(soc, "Connection: Close\r\n");
  84. }
  85. for (i=0;i<(extraHeadSize/25);i++) {
  86. sockprintf(soc, "Comment%04u: 1234567890\r\n", i % 10000);
  87. }
  88. if (!strcmp(METHOD, "GET")) {
  89. sockprintf(soc, "\r\n");
  90. } else if (chunked) {
  91. sockprintf(soc, "Transfer-Encoding: chunked\r\n\r\n", postSize);
  92. for (i=0;i<(postSize/10);i++) {sockprintf(soc, "A\r\n1234567890\r\n");}
  93. if ((postSize%10)>0) {
  94. sockprintf(soc, "%x\r\n", postSize%10);
  95. for (i=0;i<(postSize%10);i++) {sockprintf(soc, "_");}
  96. sockprintf(soc, "\r\n");
  97. }
  98. } else {
  99. sockprintf(soc, "Content-Length: %u\r\n\r\n", postSize);
  100. for (i=0;i<postSize/10;i++) {sockprintf(soc, "1234567890");}
  101. for (i=0;i<postSize%10;i++) {sockprintf(soc, ".");}
  102. timeOut += postSize/10000;
  103. }
  104. if (!keep_alive) {
  105. shutdown(soc, SD_SEND);
  106. } else {
  107. timeOut = 2;
  108. }
  109. // wait for response from the server
  110. bodyData = totalData = 0;
  111. isBody = 0;
  112. lastData = time(0);
  113. for (;;) {
  114. char buf[20480];
  115. int chunkSize = 0;
  116. unsigned long dataReady = 0;
  117. Sleep(1);
  118. if (ioctlsocket(soc, FIONREAD, &dataReady) < 0) break;
  119. if (dataReady) {
  120. chunkSize = recv(soc, buf+totalData, sizeof(buf)-totalData, 0);
  121. if (chunkSize<0) {
  122. printf("Error: recv failed for client %i\r\n", (int)clientNo);
  123. break;
  124. } else if (!isBody) {
  125. char * headEnd = strstr(buf,"\xD\xA\xD\xA");
  126. if (headEnd) {
  127. headEnd+=4;
  128. chunkSize -= ((int)headEnd - (int)buf);
  129. if (chunkSize>0) {
  130. //fwrite(headEnd,1,got,STORE);
  131. bodyData += chunkSize;
  132. }
  133. isBody=1;
  134. }
  135. } else {
  136. //fwrite(buf,1,got,STORE);
  137. bodyData += chunkSize;
  138. }
  139. lastData = time(0);
  140. totalData += chunkSize;
  141. } else {
  142. time_t current = time(0);
  143. if (difftime(current, lastData) > timeOut) {
  144. break;
  145. }
  146. Sleep(10);
  147. }
  148. }
  149. shutdown(soc, SD_BOTH);
  150. closesocket(soc);
  151. return 0;
  152. }
  153. int main(int argc, char * argv[]) {
  154. HOSTENT * lpHost = 0;
  155. #ifdef WIN32
  156. WSADATA wsaData = {0};
  157. if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
  158. printf("\r\nCannot init WinSock\a\r\n");
  159. return 1;
  160. }
  161. #endif
  162. lpHost = gethostbyname(HOST);
  163. if (lpHost == NULL) {
  164. printf("\r\nCannot find host %s\a\r\n",HOST);
  165. return 2;
  166. }
  167. target.sin_family = AF_INET;
  168. #ifdef WIN32
  169. target.sin_addr.s_addr = *((u_long FAR *) (lpHost->h_addr));
  170. #else
  171. target.sin_addr.s_addr = htonl(0x7F000001);
  172. #endif
  173. target.sin_port = htons(PORT);
  174. printf("\r\nConnect to %s\r\n", inet_ntoa(target.sin_addr));
  175. TestClient(1);
  176. #ifdef WIN32
  177. WSACleanup();
  178. #endif
  179. return 0;
  180. }