#ifndef TILE_RPC_PROTOCOL_HTTP_BUFFER_IO_H
#define TILE_RPC_PROTOCOL_HTTP_BUFFER_IO_H

#pragma once

#include "tile/base/buffer.h"
#include "tile/base/slice.h"

namespace tile {
enum class HttpVersion;
enum class HttpMethod;
enum class HttpStatus;
class HttpRequest;
class HttpHeaders;
class HttpResponse;

}// namespace tile

namespace tile {
namespace http {
enum class ReadStatus { OK, NoEnoughData, UnexpectedFormat, Error };

using HeaderBlock = std::pair<std::unique_ptr<char[]>, std::size_t>;

// OK:
//   - start-line
//   - *(message-header CRLF)
//   - CRLF
ReadStatus ReadHeader(const NoncontiguousBuffer &buffer, HeaderBlock *storage);

Slice ReadFieldFromRawBytes(Slice view, Slice key);
bool ParseMessagePartial(HeaderBlock &&storage, Slice *start_line, HttpHeaders *headers);
// bool ParseMessagePartial(HeaderBlock &&storage, Slice *start_line,
//                          HttpHeaders *headers);
bool ParseRequestStartLine(Slice s, HttpVersion *version, HttpMethod *method, Slice *uri);
bool ParseResponseStartLine(Slice s, HttpStatus *code);

void WriteMessage(const HttpRequest &req, NoncontiguousBufferBuilder *builder);
void WriteMessage(const HttpResponse &resp, NoncontiguousBufferBuilder *builder);

}// namespace http
}// namespace tile

#endif// TILE_RPC_PROTOCOL_HTTP_BUFFER_IO_H
