test_messages.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #ifndef _TEST_MESSAGES_H_
  23. #define _TEST_MESSAGES_H_
  24. #define TEST_MSG_200 \
  25. "HTTP/1.1 200 OK\r\n" \
  26. "Date: Wed, 10 Mar 2010 13:37:13 GMT\r\n" \
  27. "Server: Apache\r\n" \
  28. "Content-length: 3\r\n" \
  29. "Expires: Sat, 07 Aug 2010 13:37:13 +0000\r\n" \
  30. "Cache-Control: public\r\n" \
  31. "X-Deezer-Cache: HIT\r\n" \
  32. "Pragma: \r\n" \
  33. "Keep-Alive: timeout=2, max=100\r\n" \
  34. "Connection: Keep-Alive\r\n" \
  35. "Content-Type: video/flv\r\n" \
  36. "\r\n" \
  37. "123"
  38. #define TEST_MSG_401 \
  39. "HTTP/1.1 401 Unauthorized\r\n" \
  40. "Server: Apache-Coyote/1.1\r\n" \
  41. "WWW-Authenticate: Digest realm=\"example.com\", qop=\"auth\", nonce=\"f39d20a4dbfbd32d943de9b285d59a30\", opaque=\"c5ad02feb52eb050e464cec3740c0f2e\"\r\n" \
  42. "WWW-Authenticate: Basic realm=\"WallyWorld\"\r\n" \
  43. "Transfer-Encoding: chunked;test=21;tes7\r\n" \
  44. "Date: Wed, 10 Mar 2010 14:20:47 GMT\r\n" \
  45. "Etag: W/\"1231-3213213\"\r\n" \
  46. "\r\n"
  47. #define TEST_MSG_WS \
  48. "Upgrade: websocket\r\n" \
  49. "Connection: Upgrade\r\n" \
  50. "Host: 192.168.0.11:5060\r\n" \
  51. "Origin: null\r\n" \
  52. "Sec-WebSocket-Key: Z8DmZQ9jZqK7vJqXbsDlYw==\r\n" \
  53. "Sec-WebSocket-Version: 13, 5, 6\r\n" \
  54. "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" \
  55. "\r\n"
  56. #define TEST_MSG_OPTIONS \
  57. "OPTIONS /85 HTTP/1.1\r\n" \
  58. "Host: 192.168.0.37:10063\r\n" \
  59. "Connection: keep-alive\r\n" \
  60. "Access-Control-Request-Method: POST\r\n" \
  61. "Origin: null\r\n" \
  62. "User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17\r\n" \
  63. "Access-Control-Request-Headers: connection, origin, content-type\r\n" \
  64. "Accept: */*\r\n" \
  65. "Accept-Encoding: gzip,deflate,sdch\r\n" \
  66. "Accept-Language: en-US,en;q=0.8\r\n" \
  67. "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n" \
  68. "\r\n"
  69. #define TEST_MSG TEST_MSG_OPTIONS
  70. static void test_messages()
  71. {
  72. thttp_message_t *message = tsk_null;
  73. tsk_ragel_state_t state;
  74. int ret/*, idx*/;
  75. const char* msg_start = TEST_MSG_WS;
  76. const char* msg_end = msg_start + tsk_strlen(msg_start);
  77. //const thttp_header_Sec_WebSocket_Key_t* ws_hdr_key;
  78. //const thttp_header_Sec_WebSocket_Version_t* ws_hdr_version;
  79. //const thttp_header_Sec_WebSocket_Accept_t* ws_hdr_accept;
  80. message = thttp_message_create();
  81. /*while(msg_start < msg_end){
  82. if((idx = tsk_strindexOf(msg_start, (msg_end - msg_start), "\r\n")) <= 2){
  83. break;
  84. }
  85. idx+= 2;
  86. tsk_ragel_state_init(&state, msg_start, idx);
  87. if((ret = thttp_header_parse(&state, message))){
  88. break;
  89. }
  90. msg_start += idx;
  91. }
  92. if((ws_hdr_key = (const thttp_header_Sec_WebSocket_Key_t*)thttp_message_get_header(message, thttp_htype_Sec_WebSocket_Key))){
  93. TSK_DEBUG_INFO("Sec-WebSocket-Key: %s", ws_hdr_key->value);
  94. }
  95. if((ws_hdr_accept = (const thttp_header_Sec_WebSocket_Accept_t*)thttp_message_get_header(message, thttp_htype_Sec_WebSocket_Accept))){
  96. TSK_DEBUG_INFO("Sec-WebSocket-Accept: %s", ws_hdr_accept->value);
  97. }
  98. if((ws_hdr_version = (const thttp_header_Sec_WebSocket_Version_t*)thttp_message_get_header(message, thttp_htype_Sec_WebSocket_Version))){
  99. const tsk_list_item_t* item;
  100. tsk_list_foreach(item, ws_hdr_version->values){
  101. TSK_DEBUG_INFO("Sec-WebSocket-Version: %s", TSK_STRING_STR(item->data));
  102. }
  103. }*/
  104. /* deserialize the message */
  105. tsk_ragel_state_init(&state, TEST_MSG, strlen(TEST_MSG));
  106. if(!(ret = thttp_message_parse(&state, &message, tsk_true))) {
  107. tsk_buffer_t *buffer = tsk_buffer_create_null();
  108. /* serialize the message */
  109. thttp_message_serialize(message, buffer);
  110. TSK_DEBUG_INFO("HTTP Message=\n%s", TSK_BUFFER_TO_STRING(buffer));
  111. TSK_OBJECT_SAFE_FREE(buffer);
  112. }
  113. else {
  114. TSK_DEBUG_ERROR("Failed to parse HTTP message.");
  115. }
  116. TSK_OBJECT_SAFE_FREE(message);
  117. getchar();
  118. }
  119. #endif /* _TEST_MESSAGES_H_ */