HTTP server no adhears to the correct carrage return of \r\n

This commit is contained in:
Ziver Koc 2026-04-04 23:50:36 +02:00
parent 7209d677a4
commit 8a7bc3362e
3 changed files with 47 additions and 43 deletions

View file

@ -10,7 +10,7 @@ plugins {
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2025 Ziver Koc * Copyright (c) 2025-2026 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -82,6 +82,7 @@ java {
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation" options.compilerArgs << "-Xlint:deprecation"
options.compilerArgs << "-Xlint:unchecked"
} }
sourceSets { sourceSets {

View file

@ -1,7 +1,7 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020 Ziver Koc * Copyright (c) 2020-2026 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -24,8 +24,6 @@
package zutil.net.http; package zutil.net.http;
import zutil.converter.Converter;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.HashMap; import java.util.HashMap;
@ -37,6 +35,8 @@ import java.util.HashMap;
* @author Ziver * @author Ziver
*/ */
public class HttpPrintStream extends OutputStream { public class HttpPrintStream extends OutputStream {
protected static final String LINE_SEPARATOR = "\r\n";
/** /**
* Specifies if the HTTP message is a request (client) or a response (server). * Specifies if the HTTP message is a request (client) or a response (server).
*/ */
@ -60,7 +60,7 @@ public class HttpPrintStream extends OutputStream {
/** /**
* Creates an new instance of HttpPrintStream with * Creates a new instance of HttpPrintStream with
* message type of RESPONSE and buffering disabled. * message type of RESPONSE and buffering disabled.
* *
* @param out is the OutputStream where the data will be written to * @param out is the OutputStream where the data will be written to
@ -70,7 +70,7 @@ public class HttpPrintStream extends OutputStream {
} }
/** /**
* Creates an new instance of HttpPrintStream with * Creates a new instance of HttpPrintStream with
* message type buffering disabled. * message type buffering disabled.
* *
* @param out is the OutputStream where the data will be written to * @param out is the OutputStream where the data will be written to
@ -101,6 +101,8 @@ public class HttpPrintStream extends OutputStream {
* is enabled until you close or flush the stream. * is enabled until you close or flush the stream.
* This function will flush the stream if buffering is * This function will flush the stream if buffering is
* disabled. * disabled.
*
* @param enable true if buffering is enabled, false otherwise
*/ */
public void enableBuffering(boolean enable) { public void enableBuffering(boolean enable) {
if (enable && !isBufferEnabled()) { if (enable && !isBufferEnabled()) {
@ -181,7 +183,7 @@ public class HttpPrintStream extends OutputStream {
} }
/** /**
* Adds an header value * Adds a header value
* *
* @param key is the header name * @param key is the header name
* @param value is the value of the header * @param value is the value of the header
@ -206,18 +208,18 @@ public class HttpPrintStream extends OutputStream {
* Prints a new line * Prints a new line
*/ */
public void println() { public void println() {
printOrBuffer(System.lineSeparator()); printOrBuffer(LINE_SEPARATOR);
} }
/** /**
* Prints with a new line * Prints with a new line
*/ */
public void println(String s) { public void println(String s) {
printOrBuffer(s + System.lineSeparator()); printOrBuffer(s + LINE_SEPARATOR);
} }
/** /**
* Prints an string * Prints a string
*/ */
public void print(String s) { public void print(String s) {
printOrBuffer(s); printOrBuffer(s);
@ -248,12 +250,13 @@ public class HttpPrintStream extends OutputStream {
header.getResponseStatusCode() + " " + header.getResponseStatusCode() + " " +
header.getResponseStatusString()); header.getResponseStatusString());
} }
out.println(); out.print(LINE_SEPARATOR);
// Send headers // Send headers
for (String key : header.getHeaderMap().keySet()) { for (String key : header.getHeaderMap().keySet()) {
out.println(key + ": " + header.getHeader(key)); out.print(key + ": " + header.getHeader(key));
out.print(LINE_SEPARATOR);
} }
// Send cookies // Send cookies
@ -264,16 +267,16 @@ public class HttpPrintStream extends OutputStream {
for (String key : header.getCookieMap().keySet()) { for (String key : header.getCookieMap().keySet()) {
out.print(" " + key + "=" + header.getCookie(key) + ";"); out.print(" " + key + "=" + header.getCookie(key) + ";");
} }
out.println(); out.print(LINE_SEPARATOR);
} else { } else {
for (String key : header.getCookieMap().keySet()) { for (String key : header.getCookieMap().keySet()) {
out.print(HttpHeader.HEADER_SET_COOKIE + ": " + key + "=" + header.getCookie(key) + ";"); out.print(HttpHeader.HEADER_SET_COOKIE + ": " + key + "=" + header.getCookie(key) + ";");
out.println(); out.print(LINE_SEPARATOR);
} }
} }
} }
out.println(); out.print(LINE_SEPARATOR);
header = null; header = null;
// Check for errors // Check for errors

View file

@ -1,7 +1,7 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020 Ziver Koc * Copyright (c) 2020-2026 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -39,8 +39,8 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"GET / HTTP/1.0" + System.lineSeparator() + "GET / HTTP/1.0" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -54,8 +54,8 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"POST / HTTP/1.0" + System.lineSeparator() + "POST / HTTP/1.0" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -69,8 +69,8 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"GET /test/path/to/page?param=aa&tt=aa HTTP/1.0" + System.lineSeparator() + "GET /test/path/to/page?param=aa&tt=aa HTTP/1.0" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -84,9 +84,9 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"GET / HTTP/1.0" + System.lineSeparator() + "GET / HTTP/1.0" + HttpPrintStream.LINE_SEPARATOR +
"Cookie: Test=value;" + System.lineSeparator() + "Cookie: Test=value;" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -101,9 +101,9 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"GET / HTTP/1.0" + System.lineSeparator() + "GET / HTTP/1.0" + HttpPrintStream.LINE_SEPARATOR +
"Cookie: Test1=value1; Test2=value2;" + System.lineSeparator() + "Cookie: Test1=value1; Test2=value2;" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -116,8 +116,8 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"HTTP/1.0 200 OK" + System.lineSeparator() + "HTTP/1.0 200 OK" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -131,8 +131,8 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"HTTP/1.0 400 Bad Request" + System.lineSeparator() + "HTTP/1.0 400 Bad Request" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -146,9 +146,9 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"HTTP/1.0 200 OK" + System.lineSeparator() + "HTTP/1.0 200 OK" + HttpPrintStream.LINE_SEPARATOR +
"Set-Cookie: Test=value;" + System.lineSeparator() + "Set-Cookie: Test=value;" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -163,10 +163,10 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"HTTP/1.0 200 OK" + System.lineSeparator() + "HTTP/1.0 200 OK" + HttpPrintStream.LINE_SEPARATOR +
"Set-Cookie: Test1=value1;" + System.lineSeparator() + "Set-Cookie: Test1=value1;" + HttpPrintStream.LINE_SEPARATOR +
"Set-Cookie: Test2=value2;" + System.lineSeparator() + "Set-Cookie: Test2=value2;" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }
@ -182,10 +182,10 @@ public class HttpPrintStreamTest {
httpOut.flush(); httpOut.flush();
assertEquals( assertEquals(
"HTTP/1.0 200 OK" + System.lineSeparator() + "HTTP/1.0 200 OK" + HttpPrintStream.LINE_SEPARATOR +
"Test1: value1" + System.lineSeparator() + "Test1: value1" + HttpPrintStream.LINE_SEPARATOR +
"Test2: value2" + System.lineSeparator() + "Test2: value2" + HttpPrintStream.LINE_SEPARATOR +
System.lineSeparator(), HttpPrintStream.LINE_SEPARATOR,
out.toString() out.toString()
); );
} }