internal WebAsyncResult SetHeadersAsync(bool setInternalLength, AsyncCallback callback, object state) { if (headersSent) { return(null); } string method = request.Method; bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" || method == "TRACE"); bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" || method == "COPY" || method == "MOVE" || method == "LOCK" || method == "UNLOCK"); if (setInternalLength && !no_writestream && writeBuffer != null) { request.InternalContentLength = writeBuffer.Length; } if (sendChunked || request.ContentLength > -1 || no_writestream || webdav) { headersSent = true; headers = request.GetRequestHeaders(); var result = new WebAsyncResult(callback, state); result.InnerAsyncResult = cnc.BeginWrite(request, headers, 0, headers.Length, new AsyncCallback(SetHeadersCB), result); if (result.InnerAsyncResult != null) { return(result); } } return(null); }
bool SetHeadersAsync(SimpleAsyncResult result, bool setInternalLength) { if (headersSent) { return(false); } string method = request.Method; bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" || method == "TRACE"); bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" || method == "COPY" || method == "MOVE" || method == "LOCK" || method == "UNLOCK"); if (setInternalLength && !no_writestream && writeBuffer != null) { request.InternalContentLength = writeBuffer.Length; } bool has_content = !no_writestream && (writeBuffer == null || request.ContentLength > -1); if (!(sendChunked || has_content || no_writestream || webdav)) { return(false); } headersSent = true; headers = request.GetRequestHeaders(); var innerResult = cnc.BeginWrite(request, headers, 0, headers.Length, r => { try { cnc.EndWrite(request, true, r); if (!initRead) { initRead = true; cnc.InitRead(); } var cl = request.ContentLength; if (!sendChunked && cl == 0) { requestWritten = true; } result.SetCompleted(false); } catch (WebException e) { result.SetCompleted(false, e); } catch (Exception e) { result.SetCompleted(false, new WebException("Error writing headers", WebExceptionStatus.SendFailure, WebExceptionInternalStatus.RequestFatal, e)); } }, null); return(innerResult != null); }
internal void WriteRequest() { if (requestWritten) { return; } requestWritten = true; if (sendChunked) { return; } if (!allowBuffering || writeBuffer == null) { return; } byte [] bytes = writeBuffer.GetBuffer(); int length = (int)writeBuffer.Length; if (request.ContentLength != -1 && request.ContentLength < length) { nextReadCalled = true; cnc.Close(true); throw new WebException("Specified Content-Length is less than the number of bytes to write", null, WebExceptionStatus.ServerProtocolViolation, null); } if (!headersSent) { string method = request.Method; bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" || method == "TRACE"); if (!no_writestream) { request.InternalContentLength = length; } byte[] requestHeaders = request.GetRequestHeaders(); WebAsyncResult ar = new WebAsyncResult(null, null); SetHeadersAsync(requestHeaders, ar); ar.AsyncWaitHandle.WaitOne(); if (ar.Exception != null) { throw ar.Exception; } } if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100) { return; } IAsyncResult result = null; if (length > 0) { result = cnc.BeginWrite(request, bytes, 0, length, null, null); } if (!initRead) { initRead = true; WebConnection.InitRead(cnc); } if (length > 0) { complete_request_written = cnc.EndWrite(request, false, result); } else { complete_request_written = true; } }
bool SetHeadersAsync(SimpleAsyncResult result, bool setInternalLength) { if (headersSent) { return(false); } bool webdav = false; bool writestream = false; switch (request.Method) { case "PROPFIND": case "PROPPATCH": case "MKCOL": case "COPY": case "MOVE": case "LOCK": case "UNLOCK": webdav = true; break; case "POST": case "PUT": writestream = true; break; } if (setInternalLength && writestream && writeBuffer != null) { request.InternalContentLength = writeBuffer.Length; } if (!(sendChunked || request.ContentLength > -1 || !writestream || webdav)) { return(false); } headersSent = true; headers = request.GetRequestHeaders(); var innerResult = cnc.BeginWrite(request, headers, 0, headers.Length, r => { try { cnc.EndWrite(request, true, r); if (!initRead) { initRead = true; WebConnection.InitRead(cnc); } var cl = request.ContentLength; if (!sendChunked && cl == 0) { requestWritten = true; } result.SetCompleted(false); } catch (WebException e) { result.SetCompleted(false, e); } catch (Exception e) { result.SetCompleted(false, new WebException("Error writing headers", e, WebExceptionStatus.SendFailure)); } }, null); return(innerResult != null); }