| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Net.Quic; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// The exception thrown when an HTTP/2 or an HTTP/3 protocol error occurs. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// When calling <see cref="HttpClient"/> or <see cref="SocketsHttpHandler"/> methods, <see cref="HttpProtocolExcept |
| | | 14 | | /// <see cref="HttpRequestException"/> if a protocol error occurs. |
| | | 15 | | /// When calling <see cref="Stream"/> methods on the stream returned by <see cref="HttpContent.ReadAsStream()"/> or |
| | | 16 | | /// <see cref="HttpContent.ReadAsStreamAsync(Threading.CancellationToken)"/>, <see cref="HttpProtocolException"/> ca |
| | | 17 | | /// </remarks> |
| | | 18 | | public sealed class HttpProtocolException : HttpIOException |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="HttpProtocolException"/> class with the specified error code, |
| | | 22 | | /// message, and inner exception. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="errorCode">The HTTP/2 or HTTP/3 error code.</param> |
| | | 25 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | | 26 | | /// <param name="innerException">The exception that is the cause of the current exception.</param> |
| | | 27 | | public HttpProtocolException(long errorCode, string message, Exception? innerException) |
| | 0 | 28 | | : base(Http.HttpRequestError.HttpProtocolError, message, innerException) |
| | 0 | 29 | | { |
| | 0 | 30 | | ErrorCode = errorCode; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the HTTP/2 or HTTP/3 error code associated with this exception. |
| | | 35 | | /// </summary> |
| | 0 | 36 | | public long ErrorCode { get; } |
| | | 37 | | |
| | | 38 | | #if !TARGET_BROWSER && !TARGET_WASI |
| | | 39 | | internal static HttpProtocolException CreateHttp2StreamException(Http2ProtocolErrorCode protocolError) |
| | 0 | 40 | | { |
| | 0 | 41 | | string message = SR.Format(SR.net_http_http2_stream_error, GetName(protocolError), ((int)protocolError).ToSt |
| | 0 | 42 | | return new HttpProtocolException((long)protocolError, message, null); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | internal static HttpProtocolException CreateHttp2ConnectionException(Http2ProtocolErrorCode protocolError, strin |
| | 0 | 46 | | { |
| | 0 | 47 | | message = SR.Format(message ?? SR.net_http_http2_connection_error, GetName(protocolError), ((int)protocolErr |
| | 0 | 48 | | return new HttpProtocolException((long)protocolError, message, null); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static HttpProtocolException CreateHttp3StreamException(Http3ErrorCode protocolError, QuicException inn |
| | 0 | 52 | | { |
| | 0 | 53 | | string message = SR.Format(SR.net_http_http3_stream_error, GetName(protocolError), ((int)protocolError).ToSt |
| | 0 | 54 | | return new HttpProtocolException((long)protocolError, message, innerException); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | internal static HttpProtocolException CreateHttp3ConnectionException(Http3ErrorCode protocolError, string? messa |
| | 0 | 58 | | { |
| | 0 | 59 | | message = SR.Format(message ?? SR.net_http_http3_connection_error, GetName(protocolError), ((int)protocolErr |
| | 0 | 60 | | return new HttpProtocolException((long)protocolError, message, null); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | private static string GetName(Http2ProtocolErrorCode code) => |
| | | 64 | | // These strings are the names used in the HTTP2 spec and should not be localized. |
| | 0 | 65 | | code switch |
| | 0 | 66 | | { |
| | 0 | 67 | | Http2ProtocolErrorCode.NoError => "NO_ERROR", |
| | 0 | 68 | | Http2ProtocolErrorCode.ProtocolError => "PROTOCOL_ERROR", |
| | 0 | 69 | | Http2ProtocolErrorCode.InternalError => "INTERNAL_ERROR", |
| | 0 | 70 | | Http2ProtocolErrorCode.FlowControlError => "FLOW_CONTROL_ERROR", |
| | 0 | 71 | | Http2ProtocolErrorCode.SettingsTimeout => "SETTINGS_TIMEOUT", |
| | 0 | 72 | | Http2ProtocolErrorCode.StreamClosed => "STREAM_CLOSED", |
| | 0 | 73 | | Http2ProtocolErrorCode.FrameSizeError => "FRAME_SIZE_ERROR", |
| | 0 | 74 | | Http2ProtocolErrorCode.RefusedStream => "REFUSED_STREAM", |
| | 0 | 75 | | Http2ProtocolErrorCode.Cancel => "CANCEL", |
| | 0 | 76 | | Http2ProtocolErrorCode.CompressionError => "COMPRESSION_ERROR", |
| | 0 | 77 | | Http2ProtocolErrorCode.ConnectError => "CONNECT_ERROR", |
| | 0 | 78 | | Http2ProtocolErrorCode.EnhanceYourCalm => "ENHANCE_YOUR_CALM", |
| | 0 | 79 | | Http2ProtocolErrorCode.InadequateSecurity => "INADEQUATE_SECURITY", |
| | 0 | 80 | | Http2ProtocolErrorCode.Http11Required => "HTTP_1_1_REQUIRED", |
| | 0 | 81 | | _ => "(unknown error)", |
| | 0 | 82 | | }; |
| | | 83 | | |
| | | 84 | | private static string GetName(Http3ErrorCode code) => |
| | | 85 | | // These strings come from the H3 spec and should not be localized. |
| | 0 | 86 | | code switch |
| | 0 | 87 | | { |
| | 0 | 88 | | Http3ErrorCode.NoError => "H3_NO_ERROR", |
| | 0 | 89 | | Http3ErrorCode.ProtocolError => "H3_GENERAL_PROTOCOL_ERROR", |
| | 0 | 90 | | Http3ErrorCode.InternalError => "H3_INTERNAL_ERROR", |
| | 0 | 91 | | Http3ErrorCode.StreamCreationError => "H3_STREAM_CREATION_ERROR", |
| | 0 | 92 | | Http3ErrorCode.ClosedCriticalStream => "H3_CLOSED_CRITICAL_STREAM", |
| | 0 | 93 | | Http3ErrorCode.UnexpectedFrame => "H3_FRAME_UNEXPECTED", |
| | 0 | 94 | | Http3ErrorCode.FrameError => "H3_FRAME_ERROR", |
| | 0 | 95 | | Http3ErrorCode.ExcessiveLoad => "H3_EXCESSIVE_LOAD", |
| | 0 | 96 | | Http3ErrorCode.IdError => "H3_ID_ERROR", |
| | 0 | 97 | | Http3ErrorCode.SettingsError => "H3_SETTINGS_ERROR", |
| | 0 | 98 | | Http3ErrorCode.MissingSettings => "H3_MISSING_SETTINGS", |
| | 0 | 99 | | Http3ErrorCode.RequestRejected => "H3_REQUEST_REJECTED", |
| | 0 | 100 | | Http3ErrorCode.RequestCancelled => "H3_REQUEST_CANCELLED", |
| | 0 | 101 | | Http3ErrorCode.RequestIncomplete => "H3_REQUEST_INCOMPLETE", |
| | 0 | 102 | | Http3ErrorCode.ConnectError => "H3_CONNECT_ERROR", |
| | 0 | 103 | | Http3ErrorCode.VersionFallback => "H3_VERSION_FALLBACK", |
| | 0 | 104 | | _ => "(unknown error)" |
| | 0 | 105 | | }; |
| | | 106 | | #endif |
| | | 107 | | } |
| | | 108 | | } |