protocol_client_loopback_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * ws protocol handler plugin for "client_loopback_test"
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * These test plugins are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #define LWS_DLL
  21. #define LWS_INTERNAL
  22. #include "../lib/libwebsockets.h"
  23. #include <string.h>
  24. struct per_session_data__client_loopback_test {
  25. struct lws *wsi;
  26. };
  27. /*
  28. * This is a bit fiddly...
  29. *
  30. * 0) If you want the wss:// test to work, make sure the vhost is marked with
  31. * enable-client-ssl if using lwsws, or call lws_init_vhost_client_ssl() on
  32. * the vhost if you're doing it by hand.
  33. *
  34. * 1) enable the protocol on a vhost
  35. *
  36. * "ws-protocols": [{
  37. * "client-loopback-test": {
  38. * "status": "ok"
  39. * }, ...
  40. *
  41. * the vhost should listen on 80 (ws://) or 443 (wss://)
  42. *
  43. * 2) mount the http part of the test one level down on the same vhost, eg
  44. * {
  45. * "mountpoint": "/c",
  46. * "origin": "callback://client-loopback-test"
  47. * }
  48. *
  49. * 3) Use a browser to visit the mountpoint with a URI attached for looping
  50. * back, eg, if testing on localhost
  51. *
  52. * http://localhost/c/ws://localhost
  53. * https://localhost/c/wss://localhost
  54. *
  55. * 4) The HTTP part of this test protocol will try to do the requested
  56. * ws client connection, to the same test protocol on the same
  57. * server.
  58. */
  59. static int
  60. callback_client_loopback_test(struct lws *wsi, enum lws_callback_reasons reason,
  61. void *user, void *in, size_t len)
  62. {
  63. struct lws_client_connect_info i;
  64. struct per_session_data__client_loopback_test *pss =
  65. (struct per_session_data__client_loopback_test *)user;
  66. const char *p = (const char *)in;
  67. char buf[100];
  68. int n;
  69. switch (reason) {
  70. /* HTTP part */
  71. case LWS_CALLBACK_HTTP:
  72. if (len < 10)
  73. return -1;
  74. p++;
  75. while (*p && *p != '/')
  76. p++;
  77. if (!*p) {
  78. lws_return_http_status(wsi, 400, "Arg needs to be in format ws://xxx or wss://xxx");
  79. return -1;
  80. }
  81. p++;
  82. memset(&i, 0, sizeof(i));
  83. i.context = lws_get_context(wsi);
  84. // stacked /// get resolved to /
  85. if (strncmp(p, "ws:/", 4) == 0) {
  86. i.ssl_connection = 0;
  87. i.port = 80;
  88. p += 4;
  89. } else
  90. if (strncmp(p, "wss:/", 5) == 0) {
  91. i.port = 443;
  92. i.ssl_connection = 1;
  93. p += 5;
  94. } else {
  95. sprintf(buf, "Arg %s is not in format ws://xxx or wss://xxx\n", p);
  96. lws_return_http_status(wsi, 400, buf);
  97. return -1;
  98. }
  99. i.address = p;
  100. i.path = "";
  101. i.host = p;
  102. i.origin = p;
  103. i.ietf_version_or_minus_one = -1;
  104. i.protocol = "client-loopback-test";
  105. pss->wsi = lws_client_connect_via_info(&i);
  106. if (!pss->wsi)
  107. lws_return_http_status(wsi, 401, "client-loopback-test: connect failed\n");
  108. else {
  109. lwsl_notice("client connection to %s:%d with ssl: %d started\n",
  110. i.address, i.port, i.ssl_connection);
  111. lws_return_http_status(wsi, 200, "OK");
  112. }
  113. /* either way, close the triggering http link */
  114. return -1;
  115. case LWS_CALLBACK_CLOSED_HTTP:
  116. lwsl_notice("Http part closed\n");
  117. break;
  118. /* server part */
  119. case LWS_CALLBACK_ESTABLISHED:
  120. lwsl_notice("server part: LWS_CALLBACK_ESTABLISHED\n");
  121. strcpy(buf + LWS_PRE, "Made it");
  122. n = lws_write(wsi, (unsigned char *)buf + LWS_PRE,
  123. 7, LWS_WRITE_TEXT);
  124. if (n < 7)
  125. return -1;
  126. break;
  127. /* client part */
  128. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  129. lwsl_notice("Client connection established\n");
  130. break;
  131. case LWS_CALLBACK_CLIENT_RECEIVE:
  132. strncpy(buf, in, sizeof(buf) - 1);
  133. lwsl_notice("Client connection received %ld from server '%s'\n", (long)len, buf);
  134. /* OK we are done with the client connection */
  135. return -1;
  136. default:
  137. break;
  138. }
  139. return 0;
  140. }
  141. static const struct lws_protocols protocols[] = {
  142. {
  143. "client-loopback-test",
  144. callback_client_loopback_test,
  145. sizeof(struct per_session_data__client_loopback_test),
  146. 1024, /* rx buf size must be >= permessage-deflate rx size */
  147. },
  148. };
  149. LWS_EXTERN LWS_VISIBLE int
  150. init_protocol_client_loopback_test(struct lws_context *context,
  151. struct lws_plugin_capability *c)
  152. {
  153. if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
  154. lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
  155. c->api_magic);
  156. return 1;
  157. }
  158. c->protocols = protocols;
  159. c->count_protocols = ARRAY_SIZE(protocols);
  160. c->extensions = NULL;
  161. c->count_extensions = 0;
  162. return 0;
  163. }
  164. LWS_EXTERN LWS_VISIBLE int
  165. destroy_protocol_client_loopback_test(struct lws_context *context)
  166. {
  167. return 0;
  168. }