dev.mccue.microhttp.cookies

What is it

dev.mccue.microhttp.cookies provides a utility parsing cookie headers sent in requests, specifically microhttp's Request objects.

Why use it

If you've asked a user to send you a cookie on subsequent requests, such as with dev.mccue.microhttp.setcookie, you will most likely want to interpret the data in that cookie when you get it.

This library provides the ability to do that.

Getting Started

import dev.mccue.microhttp.cookies.Cookies;
import dev.mccue.microhttp.setcookie.SetCookieHeader;
import org.microhttp.EventLoop;
import org.microhttp.Options;
import org.microhttp.Response;

import java.util.List;

void main() throws Exception {
    var eventLoop = new EventLoop((request, consumer) -> {
        var cookies = Cookies.parse(request);
        var counter = cookies.get("Counter")
                .orElse("0");
        
        var setCookieHeader = SetCookieHeader.of(
                "Counter",
                Integer.toString(Integer.parseInt(counter) + 1)
        );

        consumer.accept(
                new Response(
                        200,
                        "OK",
                        List.of(setCookieHeader),
                        counter.getBytes()
                )
        );
    });

    eventLoop.start();
    eventLoop.join();
}

<- Index