javafirefoxnetwork-programmingbrave-browser

HTTP redirect does not work in every browser


I am running an HTTP-Server with a ServerSocket and listening for Socket's that the server accepts. I want to redirect to an external site depending the request. So far I parse the request and write the response into the OutputStream of the client Socket. Running the following example code:

OutputStream os = clientSocket.getOutputStream();
os.write("HTTP/1.1 301 Moved Permanently\r\n".getBytes());
os.write("Location: http://www.google.com/\r\n".getBytes());
os.write("\r\n\r\n".getBytes());
os.flush();
clientSocket.close();

The redirection to google.com works in firefox but in brave browser it opens a new browser window (without actually navigating to the site) after notifying me with "Open xdg-open? A website wants to open this application". I am running the browser on Arch Linux. How can I make it work for brave browser as well?


Solution

  • As pointed out by sgpalit in the comments, the response should be ended by a single EOL instead of one. This apparently induced the strange behavior. Changing os.write("\r\n\r\n".getBytes()); to os.write("\r\n".getBytes()); solved the problem.