CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-eclipse-jetty--jetty-http

Core HTTP protocol support library for Eclipse Jetty providing header handling, parsing, generation, compliance validation, and content processing capabilities

Pending
Overview
Eval results
Files

http-parsing-generation.mddocs/

HTTP Parsing and Generation

High-performance HTTP message parsing and generation with configurable compliance modes, efficient buffer management, and support for both request and response processing.

Capabilities

HttpParser Class

High-performance HTTP message parser with compliance validation and handler-based processing.

/**
 * HTTP message parser with compliance validation
 */
class HttpParser {
    /** Create parser for HTTP requests */
    HttpParser(RequestHandler requestHandler);
    
    /** Create parser for HTTP responses */
    HttpParser(ResponseHandler responseHandler);
    
    /** Create request parser with header size limit */
    HttpParser(RequestHandler requestHandler, int maxHeaderSize);
    
    /** Create response parser with compliance mode */
    HttpParser(ResponseHandler responseHandler, HttpCompliance compliance);
    
    /** Create parser with full configuration */
    HttpParser(RequestHandler requestHandler, int maxHeaderSize, 
              HttpCompliance compliance);
    
    /** Parse next chunk of data from buffer */
    boolean parseNext(ByteBuffer buffer);
    
    /** Reset parser to initial state */
    void reset();
    
    /** Close parser and release resources */
    void close();
    
    /** Check if parser is at start state */
    boolean isStart();
    
    /** Check if parsing is complete */
    boolean isComplete();
    
    /** Check if parser is closed */
    boolean isClosed();
    
    /** Get number of content bytes parsed */
    int getLength();
    
    /** Get expected content length from headers */
    long getContentLength();
    
    /** Check if using chunked transfer encoding */
    boolean isChunking();
    
    /** Get current HTTP compliance mode */
    HttpCompliance getHttpCompliance();
    
    /** Get compliance violation listener */
    ComplianceViolation.Listener getComplianceViolationListener();
    
    /** Set compliance violation listener */
    void setComplianceViolationListener(ComplianceViolation.Listener listener);
    
    /**
     * Handler interface for HTTP requests
     */
    interface RequestHandler extends HttpHandler {
        /** Handle parsed request line */
        boolean startRequest(String method, String uri, HttpVersion version);
        
        /** Handle request content */
        boolean content(ByteBuffer item);
        
        /** Handle request trailers */
        boolean trailers(HttpFields trailers);
        
        /** Handle early EOF during request */
        void earlyEOF();
    }
    
    /**
     * Handler interface for HTTP responses
     */
    interface ResponseHandler extends HttpHandler {
        /** Handle parsed response status line */
        boolean startResponse(HttpVersion version, int status, String reason);
        
        /** Handle response content */
        boolean content(ByteBuffer item);
        
        /** Handle response trailers */
        boolean trailers(HttpFields trailers);
        
        /** Handle early EOF during response */
        void earlyEOF();
    }
    
    /**
     * Common handler interface
     */
    interface HttpHandler {
        /** Handle parsed header */
        boolean header(HttpField field);
        
        /** Handle end of headers */
        boolean headerComplete();
        
        /** Handle end of message */
        boolean messageComplete();
        
        /** Handle parsing errors */
        void badMessage(BadMessageException failure);
        
        /** Get header cache index for field names */
        default int getHeaderCacheSize() { return 1024; }
    }
}

HttpGenerator Class

HTTP response and request generation with efficient buffer management.

/**
 * HTTP message generator with buffer management
 */
class HttpGenerator {
    /** Create generator with default settings */
    HttpGenerator();
    
    /** Create generator with server version and powered-by options */
    HttpGenerator(boolean sendServerVersion, boolean sendXPoweredBy);
    
    /** Generate HTTP request */
    Result generateRequest(MetaData.Request info, ByteBuffer header, 
                          ByteBuffer chunk, ByteBuffer content, boolean last);
    
    /** Generate HTTP response */
    Result generateResponse(MetaData.Response info, boolean head,
                           ByteBuffer header, ByteBuffer chunk, 
                           ByteBuffer content, boolean last);
    
    /** Check if generation is complete */
    boolean isEnd();
    
    /** Check current generator state */
    boolean isState(State state);
    
    /** Check if generator is idle */
    boolean isIdle();
    
    /** Reset generator to initial state */
    void reset();
    
    /** Set connection persistence */
    void setPersistent(boolean persistent);
    
    /** Check if connection should be persistent */
    boolean isPersistent();
    
    /** Get current generator state */
    State getState();
    
    /**
     * Generator states
     */
    enum State {
        /** Initial state, ready to generate */
        START,
        
        /** Headers committed, generating body */
        COMMITTED,
        
        /** Completing message generation */
        COMPLETING,
        
        /** Generation complete */
        END
    }
    
    /**
     * Generation results indicating next action needed
     */
    enum Result {
        /** Need chunk buffer for chunked encoding */
        NEED_CHUNK,
        
        /** Need response/request info to continue */
        NEED_INFO,
        
        /** Need header buffer space */
        NEED_HEADER,
        
        /** Ready to flush buffers */
        FLUSH,
        
        /** Continue with generation */
        CONTINUE,
        
        /** Shutdown output */
        SHUTDOWN_OUT,
        
        /** Generation complete */
        DONE
    }
}

MetaData Classes

HTTP metadata for requests and responses with header field integration.

/**
 * Base metadata for HTTP messages
 */
class MetaData implements Iterable<HttpField> {
    /** Create metadata with version and fields */
    MetaData(HttpVersion version, HttpFields fields);
    
    /** Create metadata with version, fields, and content length */
    MetaData(HttpVersion version, HttpFields fields, long contentLength);
    
    /** Get HTTP version */
    HttpVersion getHttpVersion();
    
    /** Get HTTP fields */
    HttpFields getHttpFields();
    
    /** Get content length (-1 if unknown) */
    long getContentLength();
    
    /** Iterator over HTTP fields */
    Iterator<HttpField> iterator();
    
    /**
     * HTTP request metadata
     */
    static class Request extends MetaData {
        /** Create request metadata */
        Request(String method, String uri, HttpVersion version, HttpFields fields);
        
        /** Create request metadata with HttpURI */
        Request(String method, HttpURI uri, HttpVersion version, HttpFields fields);
        
        /** Create request metadata with content length */
        Request(String method, HttpURI uri, HttpVersion version, 
               HttpFields fields, long contentLength);
        
        /** Get request method */
        String getMethod();
        
        /** Get request URI */
        HttpURI getHttpURI();
        
        /** Get request URI as string */
        String getURI();
        
        /** Create new request with different URI */
        Request asImmutable();
    }
    
    /**
     * HTTP response metadata  
     */
    static class Response extends MetaData {
        /** Create response metadata */
        Response(HttpVersion version, int status, HttpFields fields);
        
        /** Create response metadata with reason phrase */
        Response(HttpVersion version, int status, String reason, HttpFields fields);
        
        /** Create response metadata with content length */
        Response(HttpVersion version, int status, String reason, 
                HttpFields fields, long contentLength);
        
        /** Get response status code */
        int getStatus();
        
        /** Get response reason phrase */
        String getReason();
        
        /** Create immutable copy */
        Response asImmutable();
    }
}

HttpCompliance Class

HTTP protocol compliance modes and violation handling.

/**
 * HTTP compliance modes and validation
 */
class HttpCompliance {
    /** Strict RFC 2616 compliance */
    static final HttpCompliance RFC2616;
    
    /** Strict RFC 7230 compliance */
    static final HttpCompliance RFC7230;
    
    /** Legacy compatibility mode */
    static final HttpCompliance LEGACY;
    
    /** Check if violation is allowed */
    boolean allows(Violation violation);
    
    /** Get allowed violations set */
    Set<Violation> getAllowed();
    
    /** Get compliance mode name */
    String getName();
    
    /**
     * HTTP compliance violations
     */
    enum Violation {
        /** Multiple content-length headers */
        MULTIPLE_CONTENT_LENGTHS,
        
        /** Transfer-encoding with content-length */
        TRANSFER_ENCODING_WITH_CONTENT_LENGTH,
        
        /** Whitespace after field name */
        WHITESPACE_AFTER_FIELD_NAME,
        
        /** Case insensitive method */
        CASE_INSENSITIVE_METHOD,
        
        /** Duplicate host headers */
        DUPLICATE_HOST_HEADERS,
        
        /** No colon after field name */
        NO_COLON_AFTER_FIELD_NAME;
    }
}

Usage Examples:

import org.eclipse.jetty.http.*;
import java.nio.ByteBuffer;

// HTTP request parsing
class MyRequestHandler implements HttpParser.RequestHandler {
    private String method;
    private String uri;
    private HttpVersion version;
    private MutableHttpFields headers = new MutableHttpFields();
    
    @Override
    public boolean startRequest(String method, String uri, HttpVersion version) {
        this.method = method;
        this.uri = uri;
        this.version = version;
        return true;
    }
    
    @Override
    public boolean header(HttpField field) {
        headers.add(field);
        return true;
    }
    
    @Override
    public boolean headerComplete() {
        // Headers parsing complete
        return true;
    }
    
    @Override
    public boolean content(ByteBuffer content) {
        // Handle request body content
        return true;
    }
    
    @Override
    public boolean messageComplete() {
        // Request parsing complete
        return true;
    }
    
    @Override
    public void badMessage(BadMessageException failure) {
        // Handle parsing errors
    }
}

// Create and use parser
MyRequestHandler handler = new MyRequestHandler();
HttpParser parser = new HttpParser(handler, 8192, HttpCompliance.RFC7230);

ByteBuffer buffer = ByteBuffer.wrap(
    "GET /api/users HTTP/1.1\r\nHost: example.com\r\n\r\n".getBytes()
);

boolean complete = parser.parseNext(buffer);

// HTTP response generation
HttpGenerator generator = new HttpGenerator();

// Create response metadata
MutableHttpFields responseHeaders = new MutableHttpFields();
responseHeaders.put(HttpHeader.CONTENT_TYPE, "application/json");
responseHeaders.put(HttpHeader.CONTENT_LENGTH, "13");

MetaData.Response response = new MetaData.Response(
    HttpVersion.HTTP_1_1, 200, "OK", responseHeaders
);

// Generate response
ByteBuffer headerBuffer = ByteBuffer.allocate(1024);
ByteBuffer contentBuffer = ByteBuffer.wrap("{\"status\":\"ok\"}".getBytes());

HttpGenerator.Result result = generator.generateResponse(
    response, false, headerBuffer, null, contentBuffer, true
);

if (result == HttpGenerator.Result.FLUSH) {
    // Send headerBuffer contents to client
    headerBuffer.flip();
    // ... send headerBuffer
    
    // Send content
    contentBuffer.flip();
    // ... send contentBuffer
}

// HTTP response parsing
class MyResponseHandler implements HttpParser.ResponseHandler {
    @Override
    public boolean startResponse(HttpVersion version, int status, String reason) {
        // Handle response status line
        return true;
    }
    
    @Override
    public boolean header(HttpField field) {
        // Handle response header
        return true;
    }
    
    @Override
    public boolean content(ByteBuffer content) {
        // Handle response body
        return true;
    }
    
    @Override
    public boolean messageComplete() {
        // Response complete
        return true;
    }
}

// Parse HTTP response
MyResponseHandler responseHandler = new MyResponseHandler();
HttpParser responseParser = new HttpParser(responseHandler);

ByteBuffer responseData = ByteBuffer.wrap(
    "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello".getBytes()
);

responseParser.parseNext(responseData);

// Compliance violation handling
ComplianceViolation.Listener violationListener = new ComplianceViolation.Listener() {
    @Override
    public void onComplianceViolation(ComplianceViolation.Mode mode, 
                                     ComplianceViolation violation, String details) {
        // Log or handle compliance violations
        System.err.println("Compliance violation: " + violation + " - " + details);
    }
};

parser.setComplianceViolationListener(violationListener);

Install with Tessl CLI

npx tessl i tessl/maven-org-eclipse-jetty--jetty-http

docs

compression-utilities.md

content-management.md

cookie-handling.md

http-headers.md

http-methods-status.md

http-parsing-generation.md

index.md

multipart-processing.md

path-mapping.md

uri-processing.md

tile.json