static void StartRequestExecution(Context ctx, CURLResource ch) { ch.StartTime = DateTime.UtcNow; var uri = TryCreateUri(ch); if (uri == null) { ch.VerboseOutput("Cannot create URI for '" + ch.Url + "'."); ch.Result = CURLResponse.CreateError(CurlErrors.CURLE_URL_MALFORMAT); } else if ( IsProtocol(ch, uri, "http", CURLConstants.CURLPROTO_HTTP) || IsProtocol(ch, uri, "https", CURLConstants.CURLPROTO_HTTPS)) { ch.VerboseOutput("Initiating HTTP(S) request."); ch.Result = null; ch.ResponseTask = ExecHttpRequestInternalAsync(ctx, ch, uri); } else { ch.VerboseOutput("The protocol '" + uri.Scheme + "' is not supported."); ch.Result = CURLResponse.CreateError(CurlErrors.CURLE_UNSUPPORTED_PROTOCOL); } }
/// <summary> /// Perform a cURL session. /// </summary> public static PhpValue curl_exec(Context ctx, CURLResource ch) { var start = DateTime.UtcNow; var uri = TryCreateUri(ch); if (uri == null) { ch.Result = CURLResponse.CreateError(CURLConstants.CURLE_URL_MALFORMAT); } else if ( string.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) || string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { ch.Result = ExecHttpRequest(ctx, ch, uri); } else { ch.Result = CURLResponse.CreateError(CURLConstants.CURLE_UNSUPPORTED_PROTOCOL); } // ch.Result.TotalTime = (DateTime.UtcNow - start); return(ch.Result.ExecValue); }
static CURLResponse ExecHttpRequest(Context ctx, CURLResource ch, Uri uri) { try { return(ExecHttpRequestInternal(ctx, ch, uri)); } catch (WebException ex) { switch (ex.Status) { case WebExceptionStatus.Timeout: return(CURLResponse.CreateError(CURLConstants.CURLE_OPERATION_TIMEDOUT, ex)); case WebExceptionStatus.TrustFailure: return(CURLResponse.CreateError(CURLConstants.CURLE_SSL_CACERT, ex)); default: return(CURLResponse.CreateError(CURLConstants.CURLE_COULDNT_CONNECT, ex)); } } catch (ProtocolViolationException ex) { return(CURLResponse.CreateError(CURLConstants.CURLE_FAILED_INIT, ex)); } catch (CryptographicException ex) { return(CURLResponse.CreateError(CURLConstants.CURLE_SSL_CERTPROBLEM, ex)); } }
protected override void FreeManaged() { // clear references this.Result = null; this.OutputTransfer = null; this.PutStream = null; this.Headers = null; this.PostFields = PhpValue.Void; // base.FreeManaged(); }
static async Task <CURLResponse> ProcessHttpResponseTask(Context ctx, CURLResource ch, Task <WebResponse> responseTask) { try { using (var response = (HttpWebResponse)responseTask.Result) { return(new CURLResponse(await ProcessResponse(ctx, ch, response), response, ch)); } } catch (AggregateException agEx) { var ex = agEx.InnerException; ch.VerboseOutput(ex.ToString()); if (ex is WebException webEx) { // TODO: ch.FailOnError ? var exception = webEx.InnerException ?? webEx; switch (webEx.Status) { case WebExceptionStatus.ProtocolError: // actually ok, 301, 500, etc .. process the response: return(new CURLResponse(await ProcessResponse(ctx, ch, (HttpWebResponse)webEx.Response), (HttpWebResponse)webEx.Response, ch)); case WebExceptionStatus.Timeout: return(CURLResponse.CreateError(CurlErrors.CURLE_OPERATION_TIMEDOUT, exception)); case WebExceptionStatus.TrustFailure: return(CURLResponse.CreateError(CurlErrors.CURLE_SSL_CACERT, exception)); case WebExceptionStatus.ConnectFailure: default: return(CURLResponse.CreateError(CurlErrors.CURLE_COULDNT_CONNECT, exception)); } } else if (ex is ProtocolViolationException) { return(CURLResponse.CreateError(CurlErrors.CURLE_FAILED_INIT, ex)); } else if (ex is CryptographicException) { return(CURLResponse.CreateError(CurlErrors.CURLE_SSL_CERTPROBLEM, ex)); } else { throw ex; } } }
protected override void FreeManaged() { // clear references this.Result = null; this.ProcessingHeaders = ProcessMethod.Ignore; this.ProcessingResponse = ProcessMethod.StdOut; this.ProcessingRequest = new ProcessMethod() { Method = ProcessMethodEnum.FILE }; this.Headers = null; this.PostFields = PhpValue.Void; // base.FreeManaged(); }
static CURLResponse ProcessHttpResponseTask(Context ctx, CURLResource ch, Task <WebResponse> responseTask) { try { using (var response = (HttpWebResponse)responseTask.Result) { return(new CURLResponse(ProcessResponse(ctx, ch, response), response)); } } catch (AggregateException agEx) { var ex = agEx.InnerException; if (ex is WebException webEx) { switch (webEx.Status) { case WebExceptionStatus.Timeout: return(CURLResponse.CreateError(CurlErrors.CURLE_OPERATION_TIMEDOUT, webEx)); case WebExceptionStatus.TrustFailure: return(CURLResponse.CreateError(CurlErrors.CURLE_SSL_CACERT, webEx)); default: return(CURLResponse.CreateError(CurlErrors.CURLE_COULDNT_CONNECT, webEx)); } } else if (ex is ProtocolViolationException) { return(CURLResponse.CreateError(CurlErrors.CURLE_FAILED_INIT, ex)); } else if (ex is CryptographicException) { return(CURLResponse.CreateError(CurlErrors.CURLE_SSL_CERTPROBLEM, ex)); } else { throw ex; } } }
static void StartRequestExecution(Context ctx, CURLResource ch) { ch.StartTime = DateTime.UtcNow; var uri = TryCreateUri(ch); if (uri == null) { ch.Result = CURLResponse.CreateError(CurlErrors.CURLE_URL_MALFORMAT); } else if ( IsProtocol(ch, uri, "http", CURLConstants.CURLPROTO_HTTP) || IsProtocol(ch, uri, "https", CURLConstants.CURLPROTO_HTTPS)) { ch.Result = null; ch.ResponseTask = ExecHttpRequestInternalAsync(ctx, ch, uri); } else { ch.Result = CURLResponse.CreateError(CurlErrors.CURLE_UNSUPPORTED_PROTOCOL); } }
static void StartRequestExecution(Context ctx, CURLResource ch) { ch.StartTime = DateTime.UtcNow; var uri = TryCreateUri(ch); if (uri == null) { ch.Result = CURLResponse.CreateError(CurlErrors.CURLE_URL_MALFORMAT); } else if ( string.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) || string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { ch.Result = null; ch.ResponseTask = ExecHttpRequestInternalAsync(ctx, ch, uri); } else { ch.Result = CURLResponse.CreateError(CurlErrors.CURLE_UNSUPPORTED_PROTOCOL); } }