Package com.gmt2001.httpwsserver
Class HttpServerPageHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<FullHttpRequest>
com.gmt2001.httpwsserver.HttpServerPageHandler
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler
Processes HTTP requests and passes successful ones to the appropriate registered final handler
- Author:
- gmt2001
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable -
Method Summary
Modifier and TypeMethodDescriptionprotected voidchannelRead0(ChannelHandlerContext ctx, FullHttpRequest req) Handles incoming HTTP requests and passes valid ones to the appropriateHttpRequestHandlerIf a handler is not available for the requested path, then404 NOT FOUNDis sent back to the clientstatic booleancheckFilePermissions(ChannelHandlerContext ctx, FullHttpRequest req, Path p, boolean directoryAllowed) Checks if the file or directory pointed to bypexists, is not hidden, is not a symlink, and is readable Sends back a404 NOT FOUNDor403 FORBIDDENon failurestatic voidderegisterHttpHandler(String path) Deregisters a HTTP URI pathstatic StringdetectContentType(String fileNameOrType) Detects the content MIME type based on the filename or manually provided type extension NOTE: This method ignores everything before the last.in the filenamevoidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) Handles exceptions that are thrown up the stackstatic voidlistDirectory(ChannelHandlerContext ctx, FullHttpRequest req, Path p) Transmits aFullHttpResponseback to the client that lists the contents of the directory pointed to bypparseCookies(HttpHeaders headers) Parses out cookies and converts them to a MapparsePost(FullHttpRequest req) Parses out post data and converts it to a Mapstatic FullHttpResponseCreates and prepares aFullHttpResponsefor transmission of a status code only If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML responsestatic FullHttpResponseprepareHttpResponse(HttpResponseStatus status, byte[] content, String fileNameOrType) Creates and prepares aFullHttpResponsefor transmission This method automatically sets theContent-TypeandContent-Lengthheaders If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html, unlessfileNameOrTypeends withjsonorxmlstatic FullHttpResponseprepareHttpResponse(HttpResponseStatus status, String content) Creates and prepares aFullHttpResponsefor transmission as text/plain This method automatically sets theContent-TypeandContent-Lengthheaders If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html, unlessfileNameOrTypeends withjsonorxmlstatic FullHttpResponseprepareHttpResponse(HttpResponseStatus status, String content, String fileNameOrType) Creates and prepares aFullHttpResponsefor transmission This method automatically sets theContent-TypeandContent-Lengthheaders If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html, unlessfileNameOrTypeends withjsonorxmlstatic voidregisterHttpHandler(String path, HttpRequestHandler handler) Registers a HTTP URI path to aHttpRequestHandlerstatic voidsendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) Transmits aFullHttpResponseback to the clientstatic voidsendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res, boolean forceclose) Transmits aFullHttpResponseback to the clientMethods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelReadMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Method Details
-
channelRead0
Handles incoming HTTP requests and passes valid ones to the appropriateHttpRequestHandlerIf a handler is not available for the requested path, then404 NOT FOUNDis sent back to the client- Specified by:
channelRead0in classSimpleChannelInboundHandler<FullHttpRequest>- Parameters:
ctx- TheChannelHandlerContextof the sessionreq- TheFullHttpRequestcontaining the request- Throws:
Exception- Passes any thrown exceptions up the stack
-
exceptionCaught
Handles exceptions that are thrown up the stack- Specified by:
exceptionCaughtin interfaceChannelHandler- Specified by:
exceptionCaughtin interfaceChannelInboundHandler- Overrides:
exceptionCaughtin classChannelInboundHandlerAdapter- Parameters:
ctx- TheChannelHandlerContextof the sessioncause- The exception
-
detectContentType
Detects the content MIME type based on the filename or manually provided type extension NOTE: This method ignores everything before the last.in the filename- Parameters:
fileNameOrType- The filename or type extension to check- Returns:
- The valid MIME type, or
text/plainif not recognized
-
prepareHttpResponse
Creates and prepares aFullHttpResponsefor transmission of a status code only If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response- Parameters:
status- TheHttpResponseStatusto return- Returns:
- A
FullHttpRequestthat is ready to transmit
-
prepareHttpResponse
Creates and prepares aFullHttpResponsefor transmission as text/plain This method automatically sets theContent-TypeandContent-Lengthheaders If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html, unlessfileNameOrTypeends withjsonorxml- Parameters:
status- TheHttpResponseStatusto returncontent- The content to send- Returns:
- A
FullHttpRequestthat is ready to transmit
-
prepareHttpResponse
public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status, String content, String fileNameOrType) Creates and prepares aFullHttpResponsefor transmission This method automatically sets theContent-TypeandContent-Lengthheaders If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html, unlessfileNameOrTypeends withjsonorxml- Parameters:
status- TheHttpResponseStatusto returncontent- The content to sendfileNameOrType- The filename or type extension for MIME type detection- Returns:
- A
FullHttpRequestthat is ready to transmit
-
prepareHttpResponse
public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status, byte[] content, String fileNameOrType) Creates and prepares aFullHttpResponsefor transmission This method automatically sets theContent-TypeandContent-Lengthheaders If the value ofstatusis in theCLIENT ERROR 4xx,SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html, unlessfileNameOrTypeends withjsonorxml- Parameters:
status- TheHttpResponseStatusto returncontent- The content to sendfileNameOrType- The filename or type extension for MIME type detection- Returns:
- A
FullHttpRequestthat is ready to transmit
-
sendHttpResponse
public static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) Transmits aFullHttpResponseback to the client- Parameters:
ctx- TheChannelHandlerContextof the sessionreq- TheFullHttpRequestcontaining the requestres- TheFullHttpResponseto transmit
-
sendHttpResponse
public static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res, boolean forceclose) Transmits aFullHttpResponseback to the client- Parameters:
ctx- TheChannelHandlerContextof the sessionreq- TheFullHttpRequestcontaining the requestres- TheFullHttpResponseto transmitforceclose- If true, connection is closed regardless of status code; otherwise, only errors or unknown status codes will explicitly close the connection
-
listDirectory
Transmits aFullHttpResponseback to the client that lists the contents of the directory pointed to byp- Parameters:
ctx- TheChannelHandlerContextof the sessionreq- TheFullHttpRequestcontaining the requestp- ThePathto the directory to list
-
checkFilePermissions
public static boolean checkFilePermissions(ChannelHandlerContext ctx, FullHttpRequest req, Path p, boolean directoryAllowed) Checks if the file or directory pointed to bypexists, is not hidden, is not a symlink, and is readable Sends back a404 NOT FOUNDor403 FORBIDDENon failure- Parameters:
ctx- TheChannelHandlerContextof the sessionreq- TheFullHttpRequestcontaining the requestp- ThePathto the file or directory to checkdirectoryAllowed- Indicates if directories are allowed. If set tofalse, will cause a403 FORBIDDENifpis a directory- Returns:
- and passed the same tests.
falseotherwise
-
registerHttpHandler
Registers a HTTP URI path to aHttpRequestHandler- Parameters:
path- The URI path to bind the handler tohandler- TheHttpRequestHandlerthat will handle the requests- Throws:
IllegalArgumentException- Ifpathis either already registered, or illegal- See Also:
-
validateUriPath
-
deregisterHttpHandler
Deregisters a HTTP URI path- Parameters:
path- The path to deregister
-
parseCookies
Parses out cookies and converts them to a Map- Parameters:
headers- TheFullHttpRequestcontaining the request- Returns:
- A Map of cookies
-
parsePost
Parses out post data and converts it to a Map- Parameters:
req- TheFullHttpRequestcontaining the request- Returns:
- A Map of post data
-