client.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #if defined(LWS_CLIENT_HTTP_PROXYING)
  26. int
  27. lws_set_proxy(struct lws_vhost *vhost, const char *proxy)
  28. {
  29. char authstring[96];
  30. int brackets = 0;
  31. char *p;
  32. if (!proxy)
  33. return -1;
  34. /* we have to deal with a possible redundant leading http:// */
  35. if (!strncmp(proxy, "http://", 7))
  36. proxy += 7;
  37. p = strrchr(proxy, '@');
  38. if (p) { /* auth is around */
  39. if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
  40. goto auth_too_long;
  41. lws_strncpy(authstring, proxy, p - proxy + 1);
  42. // null termination not needed on input
  43. if (lws_b64_encode_string(authstring, lws_ptr_diff(p, proxy),
  44. vhost->proxy_basic_auth_token,
  45. sizeof vhost->proxy_basic_auth_token) < 0)
  46. goto auth_too_long;
  47. lwsl_info(" Proxy auth in use\n");
  48. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  49. proxy = p + 1;
  50. #endif
  51. } else
  52. vhost->proxy_basic_auth_token[0] = '\0';
  53. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  54. #if defined(LWS_WITH_IPV6)
  55. /*
  56. * isolating the address / port is complicated by IPv6 overloading
  57. * the meaning of : in the address. The convention to solve it is to
  58. * put [] around the ipv6 address part, eg, "[::1]:443". This must be
  59. * parsed to "::1" as the address and the port as 443.
  60. *
  61. * IPv4 addresses like myproxy:443 continue to be parsed as normal.
  62. */
  63. if (proxy[0] == '[')
  64. brackets = 1;
  65. #endif
  66. lws_strncpy(vhost->http.http_proxy_address, proxy + brackets,
  67. sizeof(vhost->http.http_proxy_address));
  68. p = vhost->http.http_proxy_address;
  69. #if defined(LWS_WITH_IPV6)
  70. if (brackets) {
  71. /* original is IPv6 format "[::1]:443" */
  72. p = strchr(vhost->http.http_proxy_address, ']');
  73. if (!p) {
  74. lwsl_err("%s: malformed proxy '%s'\n", __func__, proxy);
  75. return -1;
  76. }
  77. *p++ = '\0';
  78. }
  79. #endif
  80. p = strchr(p, ':');
  81. if (!p && !vhost->http.http_proxy_port) {
  82. lwsl_err("http_proxy needs to be ads:port\n");
  83. return -1;
  84. }
  85. if (p) {
  86. *p = '\0';
  87. vhost->http.http_proxy_port = atoi(p + 1);
  88. }
  89. lwsl_info(" Proxy %s:%u\n", vhost->http.http_proxy_address,
  90. vhost->http.http_proxy_port);
  91. #endif
  92. return 0;
  93. auth_too_long:
  94. lwsl_err("proxy auth too long\n");
  95. return -1;
  96. }
  97. #endif