anchor.diff:      %% More complex header, one faulty \r\n (end of To: line) and
       %% two Vias not located together. Also some extra spaces after To: value
       %% and extra \r\n at the end of body.
  -    {request, "INVITE" , _, Header2, "body\r\n"} = parse(Message2, none),
  +    {request, "INVITE" , _, Header2, <<"body\r\n">>} = parse(Message2, none),
   
       io:format("test: parse/1 request - 2.2~n"),
       %% Verify To:
  
positive.code: %% Function: parse(Packet, Origin)
  %% Descrip.: Parse a packet received from the network into either a
  %%           request or a response record()
  %% Returns : {request, Request}   |
  %%           {response, Response} |
  %%           {siperror, Status, Reason}
  %%           Request  = request record()
  %%           Response = response record()
  %%           Status   = integer(), SIP status code
  %%           Reason   = string(), SIP reason phrase
  %%--------------------------------------------------------------------
  parse(Packet, Origin) ->
      case parse_packet(Packet, Origin) of
  	keepalive ->
  	    keepalive;
  	{HeaderStr, BodyStr} ->
  	    {FirstLine, Headerkeylist} = parseheader(HeaderStr),
  	    case parse_firstline(FirstLine) of
  		{request, Parsed} ->
  		    %% XXX move data from SIP URI:s such as
  		    %%   sips:alice@atlanta.com?subject=project%20x&priority=urgent
  		    %% into Headerkeylist and strip them from the URI. See
  		    %% RFC 3261 19.1.5 Forming Requests from a URI
  		    request(Parsed, Headerkeylist, BodyStr);
  		{response, Parsed} ->
  		    response(Parsed, Headerkeylist, BodyStr);
  		none ->
  		    {siperror, 400, "Completely unparseable request/response"}
  	    end
      end.
  
  %%--------------------------------------------------------------------
  %% Function: parse_packet(Packet, Origin) ->
  %%           parse_packet(Packet)
  %%           Packet = string()
  %%           Origin = siporigin record() | none
  %% Descrip.: 'Fix' line breaks in Packet and then find the header-
  %%           body separator.
  %% Returns : {HeaderStr, BodyStr} |
  %%           {keepalive}
  %%           HeaderStr = string()
  %%           BodyStr   = string()
  %%--------------------------------------------------------------------
  parse_packet(Packet) ->
      parse_packet(Packet, none).
  
  parse_packet("\r\n", Origin) ->
      case sipserver:origin2str(Origin, none) of
  	none -> true;
  	S ->
  	    logger:log(debug, "Keep-alive packet from ~s", [S])
      end,
      keepalive;
  parse_packet(Packet, Origin) ->
      case sipserver:origin2str(Origin, none) of
  	none -> true;
  	S ->
  	    %% Extract receiver pid if present
  	    RStr = case Origin of
  		       _ when is_record(Origin, siporigin) ->
  
negative.code:     io:format("test: parse/1 request - 10.1~n"),
      %% make sure we can handle message where body is not yet fully received
      %% (also test \n instead of \r\n throughout the message)
      {request, "INVITE", _, Header10, <<"partial">>} = parse(Message10, none),
  
      io:format("test: parse/1 request - 10.2~n"),
      %% verify the Content-Length is untouched (to not re-introduce bug)
      ["20"] = keylist:fetch('content-length', Header10),
  
      Str11 = "INVITE sip:a@example.org SIP/2.0",
  
      io:format("test: parse/1 request - 11.1~n"),
      %% Make sure we throw the right exception on extra characters after SIP version
      {error, garbage_after_sip_version} = (catch parse(Str11 ++ "X\r\n", none)),
  
      io:format("test: parse/1 request - 11.2~n"),
      %% Make sure we throw the right exception on extra characters after SIP version
      {error, garbage_after_sip_version} = (catch parse(Str11 ++ "1\r\nTest: foo\r\n\r\n", none)),
  
      io:format("test: parse/1 request - 11.3~n"),
      %% Make sure we throw the right exception on extra characters after SIP version
      {error, garbage_after_sip_version} = (catch parse(Str11 ++ "\r\r\nTest: foo\r\n\r\n", none)),
  
      Message12 =
  	"INVITE sip:test@example.org SIP/2.0\r\n"
  	"Via: SIP/2.0/TCP two.example.org\r\n"
  	"body\r\n",
  
      io:format("test: parse/1 request - 12~n"),
      %% Only single CRLF between header and body - illegal. 'body' Can't be distinguished from another header.
      {error, no_end_of_key_or_no_header_body_separator} = (catch parse(Message12, none)),
  
      %% RESPONSES
  
      RMessage1 = "SIP/2.0 100 Trying\r\n"
  	"Via: SIP/2.0/TLS 192.0.2.78   \r\n"
  	"Content-Length: 3\r\n"
  	"\r\n"
  	"foo",
      io:format("test: parse/1 response - 1.1~n"),
      {response, 100, "Trying", RHeader1, RMessage1_body} = parse(RMessage1, none),
  
      %% verify the Via header so we know keylist is created as it should
      io:format("test: parse/1 response - 1.2~n"),
      ["SIP/2.0/TLS 192.0.2.78"] = keylist:fetch('via', RHeader1),
  
      %% verify the body
      io:format("test: parse/1 response - 1.3~n"),
      <<"foo">> = RMessage1_body,
  
      RMessage2 =
          "SIP/2.0 100 \r\n"
          "Via: SIP/2.0/TLS 192.0.2.78\r\n"
  	"\r\n",
      io:format("test: parse/1 response - 2.1~n"),
      {response, 100, "", RHeader2, <<>>} = parse(RMessage2, none),
  
      %% verify the Via header so we know keylist is created as it should
      io:format("test: parse/1 response - 2.2~n"),
      ["SIP/2.0/TLS 192.0.2.78"] = keylist:fetch('via', RHeader2),
  
      ok.
  