1 // =================================
  2 // Copyright (c) 2021 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.IO;
  8 using System.Collections;
  9 
 10 namespace System.Net.Http
 11 {
 12     public HttpStatus ReadStartOfMessage(ByteStream& streamHttpHeaderCollection& headersStreamWriter* log)
 13     {
 14         string statusLine = ReadStartOfMessageWithStatus(streamheaderslog);
 15         return HttpParser.ParseStatus(statusLine);
 16     }
 17     
 18     public string ReadStartOfMessageWithStatus(ByteStream& streamHttpHeaderCollection& headersStreamWriter* log)
 19     {
 20         if (log != null)
 21         {
 22             log->WriteLine("RESPONSE:");
 23         }
 24         bool firstLine = true;
 25         string startLine;
 26         int state = 0;
 27         int x = stream.ReadByte();
 28         string line;
 29         while (x != -1)
 30         {
 31             byte c = cast<byte>(x);
 32             switch (state)
 33             {
 34                 case 0:
 35                 {
 36                     if (c == 13u)
 37                     {
 38                         state = 1;
 39                     }
 40                     else
 41                     {
 42                         line.Append(cast<char>(c));
 43                         state = 2;
 44                     }
 45                     break;
 46                 }
 47                 case 1:
 48                 {
 49                     if (c == 10u)
 50                     {
 51                         state = 0;
 52                     }
 53                     else if (c != 13u)
 54                     {
 55                         line.Append(cast<char>(c));
 56                         state = 2;
 57                     }
 58                     break;
 59                 }
 60                 case 2:
 61                 {
 62                     if (c == 13u)
 63                     {
 64                         state = 3;
 65                     }
 66                     else
 67                     {
 68                         line.Append(cast<char>(c));
 69                     }
 70                     break;
 71                 }
 72                 case 3:
 73                 {
 74                     if (c == 10u)
 75                     {
 76                         if (log != null)
 77                         {
 78                             log->WriteLine(line);
 79                         }
 80                         if (firstLine)
 81                         {
 82                             firstLine = false;
 83                             startLine = Rvalue(line);
 84                         }
 85                         else
 86                         {
 87                             HttpHeader hdr = HttpParser.ParseHeader(line);
 88                             headers.Add(UniquePtr<HttpHeader>(new HttpHeader(hdr)));
 89                         }
 90                         line.Clear();
 91                         state = 4;
 92                     }
 93                     else if (c == 13u)
 94                     {
 95                         line.Append(cast<char>(13));
 96                         line.Append(cast<char>(13));
 97                         state = 5;
 98                     }
 99                     else
100                     {
101                         state = 2;
102                         line.Append(cast<char>(13));
103                         line.Append(cast<char>(c));
104                     }
105                     break;
106                 }
107                 case 4:
108                 {
109                     if (c == 13u)
110                     {
111                         state = 6;
112                     }
113                     else
114                     {
115                         line.Append(cast<char>(c));
116                         state = 2;
117                     }
118                     break;
119                 }
120                 case 5:
121                 {
122                     if (c == 13u)
123                     {
124                         line.Append(cast<char>(13));
125                     }
126                     else if (c == 10u)
127                     {
128                         if (log != null)
129                         {
130                             log->WriteLine(line);
131                         }
132                         if (firstLine)
133                         {
134                             firstLine = false;
135                             startLine = Rvalue(line);
136                         }
137                         else
138                         {
139                             HttpHeader hdr = HttpParser.ParseHeader(line);
140                             headers.Add(UniquePtr<HttpHeader>(new HttpHeader(hdr)));
141                         }
142                         line.Clear();
143                         state = 4;
144                     }
145                     else
146                     {
147                         line.Append(cast<char>(c));
148                         state = 2;
149                     }
150                     break;
151                 }
152                 case 6:
153                 {
154                     if (c == 10u)
155                     {
156                         return startLine;
157                     }
158                     else
159                     {
160                         line.Append(cast<char>(13));
161                         line.Append(cast<char>(c));
162                         state = 2;
163                     }
164                     break;
165                 }
166             }
167             x = stream.ReadByte();
168         }
169         throw HttpException(HttpStatus("HTTP/1.1"statusClientErrorBadRequest"invalid HTTP message"));
170     }
171 }