public virtual void OnPostProgress(HttpPostProgress arg, EHttpPostState end) { }
void parsePost() { string vcon = null; if (!_req.TryGetHeader("Content-Length", out vcon)) return; int content_len = int.Parse(vcon); string contenttype = null; if (_req.TryGetHeader("Content-Type", out contenttype)) { string lou = contenttype.ToLower(); int ix = lou.IndexOf("charset="); if (ix != -1) { string charset = contenttype.Remove(0, ix + 8); ix = charset.IndexOf(';'); if (ix != -1) charset = charset.Substring(0, ix); codec_post = Encoding.GetEncoding(charset); } bool file = lou.StartsWith("multipart/form-data"); uint MAX_POST = file ? _Server.MaxPostMultiPart : _Server.MaxPost; if (MAX_POST >= 0 && content_len > MAX_POST) throw new Exception(string.Format("POST Content-Length({0}) too big for this server", content_len)); if (file || _Server.ProgreesOnAllPost) { //progreso HttpCookie ck = null; if (httpCookie.TryGetValue(_Server.SessionCookieName, out ck)) _sessionID = ck.Value; post_pg = new HttpPostProgress(this, _Server, _IP, _req, _sessionID, content_len, file); _Server.OnPostProgress(post_pg, EHttpPostState.Start); if (post_pg.Cancel) throw new Exception("Client disconnected"); } if (file) { //multipart post HttpMultiPartParser mp = new HttpMultiPartParser(codec_post, contenttype, BUF_SIZE); mp.Process(stream, content_len, post_pg); if (mp.Files != null) _req.SetFiles(mp.Files); if (mp.HasError) throw (new Exception("Error in Post")); foreach (HttpMultiPartParser.Var v1 in mp.Vars) _req.AddPost(v1.Name, v1.Value); } else { //variables post byte[] buf = new byte[BUF_SIZE]; int to_read = content_len, numread = 0; MemoryStream ms = new MemoryStream(); try { while (to_read > 0) { numread = stream.Read(buf, 0, Math.Min(BUF_SIZE, to_read)); if (post_pg != null) //progreso { post_pg.UpdateValue(numread, true); if (post_pg.Cancel) throw new Exception("Client disconnected"); } if (numread == 0) { if (to_read == 0) break; else { throw new Exception("Client disconnected during Post"); } } to_read -= numread; ms.Write(buf, 0, numread); } } catch (Exception ex) { ms.Close(); ms.Dispose(); throw (ex); } if (ms != null) { byte[] dv = ms.ToArray(); ms.Close(); ms.Dispose(); string name, val; foreach (string s in codec_post.GetString(dv).Split('&')) { SeparaEnDos(HttpUtility.UrlDecode(s), '=', out name, out val); _req.AddPost(name, val); } } } } }
public void Process(Stream stream, int content_len, HttpPostProgress prog) { if (content_len <= 0) { return; } byte[] buf = new byte[BUF_SIZE]; int to_read = content_len, numread = 0; try { bool in_file = false; numread = stream.Read(buf, 0, BOUNDARY_LG); to_read -= numread; if (codec.GetString(buf, 0, BOUNDARY_LG) != BOUNDARY || to_read == 0) { throw (new Exception("DATA ERROR")); } byte[] last = new byte[] { }; int parm = 30000, part = 1500; if (prog != null) { prog.UpdateValue(numread, false); } StringBuilder header = new StringBuilder(); int sizen = 0; do { Array.Resize <byte>(ref last, sizen + BUF_SIZE); numread = stream.Read(last, sizen, BUF_SIZE); if (numread <= 0) { if (to_read == 0) { break; } else { throw new Exception("Client disconnected during Post"); } } to_read -= numread; sizen += numread; int ix = 0, pos = 0; while ((ix = IndexOfLine(last, pos, sizen)) != -1) { //PROCESAR LINEAS int lg = ix - pos; if (!in_file) { //HEADER if (lg == 2) { MakeHeader(header.ToString()); header.Clear(); in_file = true; } else { header.Append(codec.GetString(last, pos, lg)); } } else { if (lg == BOUNDARY_LG || lg == BOUNDARY_LGE) { string cad = codec.GetString(last, pos, lg); //BOUNDARY ENCONTRADO if (cad == BOUNDARY || cad == BOUNDARY_END) { EndMultiPart(); in_file = false; } } //archivo if (in_file) { PartMultipart(last, pos, lg); } } pos = ix; } if (pos > 0) { //ELIMINAR LO YA PROCESADO sizen -= pos; if (sizen == 0) { last = new byte[] { } } ; else { byte[] bx = new byte[sizen]; Array.Copy(last, pos, bx, 0, sizen); last = bx; } } // CONTROL DE LINEAS GRANDES if (in_file && sizen > parm) { int iput = sizen - part; PartMultipart(last, 0, iput); byte[] bx = new byte[part]; Array.Copy(last, iput, bx, 0, part); last = bx; sizen = part; } if (prog != null) //progreso { prog.UpdateValue(numread, true); if (prog.Cancel) { throw new Exception("Client disconnected"); } } } while (to_read > 0); } catch { _error = true; } finally { EndMultiPart(); } }