public void Finish(HttpTransaction transaction) { if (state == State.InKey) throw new HttpException ("Malformed POST data, key found without value."); FinishPair (transaction); }
public void HandleData(HttpTransaction transaction, ByteBuffer data, int pos, int len) { string str_data = transaction.Request.ContentEncoding.GetString (data.Bytes, pos, len); str_data = HttpUtility.HtmlDecode (str_data); pos = 0; len = str_data.Length; while (pos < len) { char c = str_data [pos++]; if (c == '&') { if (state == State.InKey) throw new InvalidOperationException ("& symbol can not be used in key data."); FinishPair (transaction); state = State.InKey; continue; } if (c == '=') { if (state == State.InValue) throw new InvalidOperationException ("= symbol can not be used in value data."); state = State.InValue; continue; } switch (state) { case State.InKey: key_buffer.Append (c); break; case State.InValue: value_buffer.Append (c); break; } } }
private void FinishPair(HttpTransaction transaction) { if (value_buffer.Length == 0) return; if (key_buffer.Length == 0) throw new HttpException ("zero length key in www-form data."); Encoding e = transaction.Request.ContentEncoding; transaction.Request.PostData.Set (HttpUtility.UrlDecode (key_buffer.ToString (), e), HttpUtility.UrlDecode (value_buffer.ToString (), e)); key_buffer.Clear (); value_buffer.Clear (); }
public static HttpTransaction BeginTransaction(HttpServer server, Socket socket, HttpConnectionCallback cb, bool closeOnEnd = false) { HttpTransaction transaction = new HttpTransaction(server, socket, cb, closeOnEnd); return(transaction); }
private void ConnectionAccepted(Socket socket) { var t = HttpTransaction.BeginTransaction(this, socket, callback, closeOnEnd); }
public void RunTransaction(HttpTransaction trans) { trans.Run(); }
private void ConnectionAccepted(object sender, ConnectionAcceptedEventArgs args) { HttpTransaction.BeginTransaction(this, args.Stream, Transaction, closeOnEnd); }
public void RemoveTransaction(HttpTransaction trans) { transactions.Remove (trans); }
private void FinishFormData(HttpTransaction transaction) { if (form_data.Count == 0) return; string data = encoding.GetString (form_data.ToArray ()); transaction.Request.PostData.Set (current_name, HttpUtility.UrlDecode (data, encoding)); form_data.Clear (); }
private void FinishFileData(HttpTransaction transaction) { if (uploaded_file == null) return; // Chop off the \r\n that gets appended before the boundary marker uploaded_file.Contents.SetLength (uploaded_file.Contents.Position - 2); uploaded_file.Finish (); transaction.Request.Files.Add (current_name, uploaded_file); uploaded_file = null; }
public void HandleData(HttpTransaction transaction, ByteBuffer data, int pos, int len) { // string str_data = encoding.GetString (data.Bytes, pos, len); byte [] str_data = data.Bytes; int begin = pos; int end = begin + len; pos = begin - 1; while (pos < end - 1 && state != State.Finished) { byte c = str_data [++pos]; switch (state) { case State.InBoundary: if (index == boundary.Length - 1) { boundary_buffer.Clear (); // Flush any data FinishFormData (transaction); FinishFileData (transaction); state = State.PostBoundary1; index = 0; break; } boundary_buffer.Add (c); if (c != boundary [index]) { // Copy the boundary buffer to the beginning and restart parsing there MemoryStream stream = new MemoryStream (); stream.Write (boundary_buffer.ToArray (), 0, boundary_buffer.Count); stream.Write (str_data, pos + 1, end - pos - 1); str_data = stream.ToArray (); pos = -1; end = str_data.Length; not_boundary = true; boundary_buffer.Clear (); state = previous_state; index = 0; // continue instead of break so not_boundary is not reset continue; } ++index; break; case State.PostBoundary1: if (c == '-') { state = State.PostBoundaryComplete; break; } if (c == '\r') break; if (c == '\n') { state = State.InHeaderKey; break; } throw new Exception (String.Format ("Invalid post boundary char '{0}'", c)); case State.PostBoundaryComplete: if (c != '-') throw new Exception (String.Format ("Invalid char '{0}' in boundary complete.", c)); state = State.Finished; break; case State.InHeaderKey: if (c == '\n') { state = current_filename == null ? State.InFormData : State.InFileData; break; } if (c == ':') { state = State.InHeaderValue; break; } header_key.Add (c); break; case State.InHeaderValue: if (c == '\r') { state = State.PostHeader1; break; } header_value.Add (c); break; case State.PostHeader1: if (c != '\n') throw new Exception (String.Format ("Invalid char '{0}' in post header 1.", c)); state = State.PostHeader2; break; case State.PostHeader2: HandleHeader (transaction); header_key.Clear (); header_value.Clear (); state = State.InHeaderKey; break; case State.InFormData: if (CheckStartingBoundary (str_data, pos)) break; form_data.Add (c); break; case State.InFileData: if (CheckStartingBoundary (str_data, pos)) break;; uploaded_file.Contents.WriteByte (c); break; default: throw new Exception (String.Format ("Unhandled state: {0}", state)); } not_boundary = false; } }
public void Finish(HttpTransaction transaction) { FinishFormData (transaction); FinishFileData (transaction); }
private void ConnectionAccepted(object sender, ConnectionAcceptedEventArgs args) { var t = HttpTransaction.BeginTransaction(this, args.Stream, callback); }
public static HttpTransaction BeginTransaction(HttpServer server, SocketStream stream, HttpConnectionCallback cb) { HttpTransaction transaction = new HttpTransaction(server, stream, cb); return(transaction); }
private void HandleHeader(HttpTransaction transaction) { string key = encoding.GetString (header_key.ToArray ()); string value = encoding.GetString (header_value.ToArray ()); if (key == "Content-Disposition") ParseContentDisposition (value); else if (key == "Content-Type") ParseContentType (value); }
public static HttpTransaction BeginTransaction(HttpServer server, SocketStream stream, HttpConnectionCallback cb) { HttpTransaction transaction = new HttpTransaction (server, stream, cb); return transaction; }
public static HttpTransaction BeginTransaction(HttpServer server, ISocketStream stream, HttpConnectionCallback cb, bool closeOnEnd = false) { HttpTransaction transaction = new HttpTransaction (server, stream, cb, closeOnEnd); return transaction; }
public static HttpTransaction BeginTransaction(HttpServer server, ITcpSocket socket, HttpConnectionCallback cb, bool closeOnEnd = false) { var transaction = new HttpTransaction(server, socket, cb, closeOnEnd); return transaction; }