Class KHTTPServerThread
java.lang.Object
java.lang.Thread
ch.k43.util.KSocketServerThread
ch.k43.util.KHTTPServerThread
- All Implemented Interfaces:
AutoCloseable
,Runnable
- Direct Known Subclasses:
KHTTPServerThreadSample
Class to handle user HTTP connections accepted by KSocketServerListener. The user class needs to override the default implementations
for the HTTP methods get(), put(), etc.
The HTTP response headers Date, Server, Content-Type, Content-Length and Connection are automatically provided unless overridden
thru setResponseHeaders().
- Since:
- 2025.04.13
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Method Summary
Modifier and TypeMethodDescriptionvoid
HTTP CONNECT method.void
HTTP DELETE method.void
HTTP GET method.int
Get maximum size of payload to be read.Get HTTP request headers.void
HTTP HEAD method.void
HTTP OPTIONS method.void
HTTP PATCH method.void
HTTP POST method.void
HTTP PUT method.void
run()
Thread main entry point (called by KSocketServerListener).boolean
Send file.boolean
Send file.boolean
Send HTML string.boolean
Send text string.boolean
Send text string.void
setMaxPayloadSize
(int argMaxSize) Set maximum payload size to be read.void
setResponseHeaders
(Properties argHeaders) Set additional headers to be sent in HTTP response.toString()
String representation of object.void
HTTP TRACE method.Methods inherited from class ch.k43.util.KSocketServerThread
close, getAuthenticatedClient, getAuthenticatedClientCN, getCiphers, getLastError, getProtocol, isConnected, isDataAvailable, isSecuredConnection, read, readLine, write, write, write, writeLine
Methods inherited from class java.lang.Thread
activeCount, checkAccess, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, threadId, yield
-
Method Details
-
delete
HTTP DELETE method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"argPayload
- HTTP payload passed with HTTP request
-
get
HTTP GET method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"
-
getMaxPayloadSize
public int getMaxPayloadSize()Get maximum size of payload to be read.- Returns:
- Size of maximum payload
-
getRequestHeaders
-
head
HTTP HEAD method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"
-
options
HTTP OPTIONS method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"argPayload
- HTTP payload passed with HTTP request
-
connect
HTTP CONNECT method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"argPayload
- HTTP payload passed with HTTP request
-
patch
HTTP PATCH method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"argPayload
- HTTP payload passed with HTTP request
-
post
HTTP POST method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"argPayload
- HTTP payload passed with HTTP request
-
put
HTTP PUT method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"argPayload
- HTTP payload passed with HTTP request
-
run
public void run()Thread main entry point (called by KSocketServerListener).- Specified by:
run
in interfaceRunnable
- Overrides:
run
in classKSocketServerThread
-
sendFile
Send file.- Parameters:
argFileName
- Name (and path) of file to be sent.- Returns:
- True for success, false otherwise
-
sendFile
Send file.- Parameters:
argFileName
- Name (and path) of file to be sentargDownload
- Tell client (browser) to locally save the file- Returns:
- True for success, false otherwise
-
sendHTML
-
sendText
-
sendText
-
setMaxPayloadSize
public void setMaxPayloadSize(int argMaxSize) Set maximum payload size to be read.- Parameters:
argMaxSize
- Maximum payload size
-
setResponseHeaders
Set additional headers to be sent in HTTP response.- Parameters:
argHeaders
- HTTP response header(s)
-
toString
String representation of object.- Overrides:
toString
in classKSocketServerThread
-
trace
HTTP TRACE method. Override this method to implement it.- Parameters:
argURL
- Passed URL e.g. "/test/s=any"argPayload
- HTTP payload passed with HTTP request
-