Add User-Agent to Websockets

I think, haven't actually tested this :D:. Closes #730.
This commit is contained in:
Jonathan Coates 2021-03-12 09:14:52 +00:00
parent ed0afc4068
commit 0ee3d10fda
2 changed files with 7 additions and 5 deletions

View File

@ -14,6 +14,7 @@
import dan200.computercraft.core.apis.http.request.HttpRequest;
import dan200.computercraft.core.apis.http.websocket.Websocket;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
@ -179,7 +180,7 @@ public final Object[] websocket( String address, Optional<Map<?, ?>> headerTbl )
}
@Nonnull
private static HttpHeaders getHeaders( @Nonnull Map<?, ?> headerTable ) throws LuaException
private HttpHeaders getHeaders( @Nonnull Map<?, ?> headerTable ) throws LuaException
{
HttpHeaders headers = new DefaultHttpHeaders();
for( Map.Entry<?, ?> entry : headerTable.entrySet() )
@ -197,6 +198,11 @@ private static HttpHeaders getHeaders( @Nonnull Map<?, ?> headerTable ) throws L
}
}
}
if( !headers.contains( HttpHeaderNames.USER_AGENT ) )
{
headers.set( HttpHeaderNames.USER_AGENT, apiEnvironment.getComputerEnvironment().getUserAgent() );
}
return headers;
}
}

View File

@ -80,10 +80,6 @@ public void channelActive( ChannelHandlerContext ctx ) throws Exception
{
request.headers().set( HttpHeaderNames.ACCEPT_CHARSET, "UTF-8" );
}
if( !request.headers().contains( HttpHeaderNames.USER_AGENT ) )
{
request.headers().set( HttpHeaderNames.USER_AGENT, this.request.environment().getComputerEnvironment().getUserAgent() );
}
request.headers().set( HttpHeaderNames.HOST, uri.getPort() < 0 ? uri.getHost() : uri.getHost() + ":" + uri.getPort() );
request.headers().set( HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE );