error.d 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. module vibe.http.internal.http2.error;
  2. import vibe.http.internal.http2.hpack.exception;
  3. import vibe.http.internal.http2.frame;
  4. import vibe.container.internal.utilallocator;
  5. import vibe.core.log;
  6. import vibe.core.net;
  7. import vibe.core.core;
  8. import vibe.core.stream;
  9. import vibe.stream.tls;
  10. import vibe.internal.array;
  11. import vibe.internal.freelistref;
  12. import vibe.internal.interfaceproxy;
  13. import std.range;
  14. import std.base64;
  15. import std.traits;
  16. import std.bitmanip; // read from ubyte (decoding)
  17. import std.typecons;
  18. import std.conv : to;
  19. import std.exception;
  20. import std.algorithm : canFind; // alpn callback
  21. import std.algorithm.iteration;
  22. enum HTTP2Error {
  23. NO_ERROR = 0x0,
  24. PROTOCOL_ERROR = 0x1,
  25. INTERNAL_ERROR = 0x2,
  26. FLOW_CONTROL_ERROR = 0x3,
  27. SETTINGS_TIMEOUT = 0x4,
  28. STREAM_CLOSED = 0x5,
  29. FRAME_SIZE_ERROR = 0x6,
  30. REFUSED_STREAM = 0x7,
  31. CANCEL = 0x8,
  32. COMPRESSION_ERROR = 0x9,
  33. CONNECT_ERROR = 0xa,
  34. ENHANCE_YOUR_CALM = 0xb,
  35. INADEQUATE_SECURITY = 0xc,
  36. HTTP_1_1_REQUIRED = 0xd
  37. }
  38. enum GOAWAYFrameLength = 17;
  39. /// creates a GOAWAY frame as defined in RFC 7540, section 6.8
  40. void buildGOAWAYFrame(R)(ref R buf, const uint streamId, HTTP2Error error)
  41. @safe @nogc if (isOutputRange!(R, ubyte))
  42. {
  43. assert(buf.length == GOAWAYFrameLength, "Unable to create GOAWAY frame");
  44. // last stream processed by the server (client-initiated)
  45. uint sid = (streamId > 1) ? streamId - 2 : 0;
  46. buf.createHTTP2FrameHeader(8, HTTP2FrameType.GOAWAY, 0x0, 0x0);
  47. buf.putBytes!4(sid & 127); // last stream ID
  48. buf.putBytes!4(error);
  49. }
  50. /// ditto
  51. void buildGOAWAYFrame(ref ubyte[GOAWAYFrameLength] dst, uint sid, HTTP2Error code)
  52. @safe @nogc {
  53. dst[].buildGOAWAYFrame(sid, code);
  54. }
  55. /// exceptions
  56. T enforceHTTP2(T)(T condition, string message = null, HTTP2Error h2e = HTTP2Error.NO_ERROR, string file = __FILE__, typeof(__LINE__) line = __LINE__) @trusted
  57. {
  58. return enforce(condition, new HTTP2Exception(message, h2e, file, line));
  59. }
  60. class HTTP2Exception : Exception
  61. {
  62. HTTP2Error code;
  63. this(string msg, HTTP2Error h2e = HTTP2Error.NO_ERROR, string file = __FILE__, size_t line = __LINE__) {
  64. code = h2e;
  65. super(msg, file, line);
  66. }
  67. }