parse static method

ParsedHttpRequest parse(
  1. String raw
)

Implementation

static ParsedHttpRequest parse(String raw) {
  final lines = raw.split('\r\n');
  final requestLine = lines.first.split(' ');
  final method = requestLine[0];
  final path = requestLine[1];

  final headers = <String, String>{};
  int i = 1;
  while (i < lines.length && lines[i].isNotEmpty) {
    final parts = lines[i].split(':');
    if (parts.length >= 2) {
      headers[parts[0].trim()] = parts.sublist(1).join(':').trim();
    }
    i++;
  }

  final body = lines.skip(i + 1).join('\n');

  return ParsedHttpRequest(
    method: method,
    path: path,
    headers: headers,
    body: body,
  );
}