diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index 463d0c3..fef8a7d 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -3374,7 +3374,16 @@ void MeshServer_ConnectEx(MeshAgentHostContainer *agent) req = ILibCreateEmptyPacket(); ILibSetVersion(req, "1.1", 3); ILibSetDirective(req, "GET", 3, path, (int)strnlen_s(path, serverUrlLen)); - ILibAddHeaderLine(req, "Host", 4, host, (int)strnlen_s(host, serverUrlLen)); + if ((port == 443 && strncmp("wss:", agent->serveruri, 4) == 0) || (port == 80 && strncmp("ws:", agent->serveruri, 3) == 0)) + { + // Default Port, so host field only contains hostname + ILibAddHeaderLine(req, "Host", 4, host, (int)strnlen_s(host, serverUrlLen)); + } + else + { + // Non default port, so host field needs to contain port number too + ILibAddHeaderLine(req, "Host", 4, ILibScratchPad, (int)sprintf_s(ILibScratchPad, sizeof(ILibScratchPad), "%s:%u", host, port)); + } free(path); diff --git a/microscript/ILibDuktape_HttpStream.c b/microscript/ILibDuktape_HttpStream.c index 61e48b5..2c6266d 100644 --- a/microscript/ILibDuktape_HttpStream.c +++ b/microscript/ILibDuktape_HttpStream.c @@ -1168,7 +1168,17 @@ duk_ret_t ILibDuktape_HttpStream_http_request(duk_context *ctx) duk_swap_top(ctx, -2); // [options][headers][concat][this] duk_push_string(ctx, ":"); // [options][headers][concat][this][:] duk_get_prop_string(ctx, -5, "port"); // [options][headers][concat][this][:][port] - duk_call_method(ctx, 2); // [options][headers][hostname] + if ((strcmp("443", (char*)duk_to_string(ctx, -1)) == 0 && isTLS == 1) || (strcmp("80", (char*)duk_to_string(ctx, -1)) == 0 && isTLS == 0)) + { + // No need to add port to host [options][headers][concat][this][:][port] + duk_pop_2(ctx); // [options][headers][concat][this] + duk_remove(ctx, -2); // [options][headers][host] + } + else + { + // Add port to host + duk_call_method(ctx, 2); // [options][headers][hostname] + } duk_put_prop_string(ctx, -2, "Host"); // [options][headers] duk_put_prop_string(ctx, -2, "headers"); // [options] }