File: CapsuleParser.cpp

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (257 lines) | stat: -rw-r--r-- 7,654 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "CapsuleParser.h"

#include "CapsuleDecoder.h"
#include "mozilla/ScopeExit.h"

namespace mozilla::net {

bool CapsuleParserListener::OnCapsule(Capsule&& aParsed) {
  mParsedCapsules.AppendElement(std::move(aParsed));
  return true;
}

void CapsuleParserListener::OnCapsuleParseFailure(nsresult aError) {
  mError = Some(aError);
}

CapsuleParser::CapsuleParser(Listener* aListener) : mListener(aListener) {}

bool CapsuleParser::ProcessCapsuleData(const uint8_t* aData, uint32_t aCount) {
  // Prevent reentrant calls: if we're already processing, just return.
  if (mProcessing) {
    return false;
  }
  mProcessing = true;

  auto resetGuard = MakeScopeExit([&]() { mProcessing = false; });

  Span<const uint8_t> input;
  if (!mBuffer.IsEmpty()) {
    mBuffer.AppendElements(aData, aCount);
    input = Span<const uint8_t>(mBuffer.Elements(), mBuffer.Length());
  } else {
    input = Span<const uint8_t>(aData, aCount);
  }
  size_t pos = 0;
  size_t length = input.Length();

  while (true) {
    Span<const uint8_t> toProcess = input.Subspan(pos, length - pos);
    auto result = ParseCapsuleData(toProcess);
    if (result.isErr()) {
      mBuffer.Clear();
      return false;
    }

    size_t processed = result.unwrap();
    if (processed == 0) {
      if (mBuffer.IsEmpty()) {
        // Store the remaining data in mBuffer.
        mBuffer.AppendElements(toProcess.Elements(), toProcess.Length());
      } else {
        // Simply remove the already processed data.
        mBuffer.RemoveElementsAt(0, pos);
      }
      break;
    }

    pos += processed;
  }

  return true;
}

Result<size_t, nsresult> CapsuleParser::ParseCapsuleData(
    Span<const uint8_t> aData) {
  if (aData.IsEmpty()) {
    return 0;
  }

  CapsuleDecoder decoder(aData.Elements(), aData.Length());
  auto type = decoder.DecodeVarint();

  if (!type) {
    return 0;
  }

  CapsuleType capsuleType = static_cast<CapsuleType>(*type);
  auto payloadLength = decoder.DecodeVarint();
  if (!payloadLength) {
    return 0;
  }

  auto payload = decoder.Decode(*payloadLength);
  if (!payload) {
    return 0;
  }

  CapsuleDecoder payloadParser(payload->Elements(), payload->Length());
  auto result =
      ParseCapsulePayload(payloadParser, capsuleType, payload->Length());
  if (result.isErr()) {
    nsresult error = result.unwrapErr();
    mListener->OnCapsuleParseFailure(error);
    return Err(error);
  }

  Capsule capsule = result.unwrap();
  if (!mListener->OnCapsule(std::move(capsule))) {
    return Err(NS_ERROR_FAILURE);
  }

  return decoder.CurrentPos();
}

Result<Capsule, nsresult> CapsuleParser::ParseCapsulePayload(
    CapsuleDecoder& aDecoder, CapsuleType aType, size_t aPayloadLength) {
  switch (aType) {
    case CapsuleType::CLOSE_WEBTRANSPORT_SESSION: {
      if (aPayloadLength < 4) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      Maybe<uint32_t> status = aDecoder.DecodeUint<uint32_t>();
      if (!status) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      // https://www.ietf.org/archive/id/draft-ietf-webtrans-http2-10.html#section-6.12
      // The reason MUST not exceed 1024 bytes.
      if (aDecoder.BytesRemaining() > 1024) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      auto reason = aDecoder.Decode(aPayloadLength - 4);
      if (!reason) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::CloseWebTransportSession(
          *status, nsCString(reinterpret_cast<const char*>(reason->Elements()),
                             reason->Length()));
    }
    case CapsuleType::DRAIN_WEBTRANSPORT_SESSION:
      break;
    case CapsuleType::PADDING:
      break;
    case CapsuleType::WT_RESET_STREAM: {
      auto id = aDecoder.DecodeVarint();
      if (!id) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      auto error = aDecoder.DecodeVarint();
      if (!error) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      auto size = aDecoder.DecodeVarint();
      if (!size) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportResetStream(*error, *size, *id);
    }
    case CapsuleType::WT_STOP_SENDING: {
      auto id = aDecoder.DecodeVarint();
      if (!id) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      auto error = aDecoder.DecodeVarint();
      if (!error) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportStopSending(*error, *id);
    }
    case CapsuleType::WT_STREAM: {
      auto id = aDecoder.DecodeVarint();
      if (!id) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      nsTArray<uint8_t> data(aDecoder.GetRemaining());
      return Capsule::WebTransportStreamData(*id, false, std::move(data));
    }
    case CapsuleType::WT_STREAM_FIN: {
      auto id = aDecoder.DecodeVarint();
      if (!id) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      nsTArray<uint8_t> data(aDecoder.GetRemaining());
      return Capsule::WebTransportStreamData(*id, true, std::move(data));
    }
    case CapsuleType::WT_MAX_DATA: {
      auto value = aDecoder.DecodeVarint();
      if (!value) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportMaxData(*value);
    }
    case CapsuleType::WT_MAX_STREAM_DATA: {
      auto id = aDecoder.DecodeVarint();
      if (!id) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      auto limit = aDecoder.DecodeVarint();
      if (!limit) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportMaxStreamData(*limit, *id);
    }
    case CapsuleType::WT_MAX_STREAMS_BIDI: {
      auto value = aDecoder.DecodeVarint();
      if (!value) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportMaxStreams(*value, true);
    }
    case CapsuleType::WT_MAX_STREAMS_UNIDI: {
      auto value = aDecoder.DecodeVarint();
      if (!value) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportMaxStreams(*value, false);
    }
    case CapsuleType::WT_DATA_BLOCKED: {
      auto limit = aDecoder.DecodeVarint();
      if (!limit) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportDataBlocked(*limit);
    }
    case CapsuleType::WT_STREAM_DATA_BLOCKED: {
      auto id = aDecoder.DecodeVarint();
      if (!id) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      auto limit = aDecoder.DecodeVarint();
      if (!limit) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportStreamDataBlocked(*limit, *id);
    }
    case CapsuleType::WT_STREAMS_BLOCKED_BIDI: {
      auto value = aDecoder.DecodeVarint();
      if (!value) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportStreamsBlocked(*value, true);
    }
    case CapsuleType::WT_STREAMS_BLOCKED_UNIDI: {
      auto value = aDecoder.DecodeVarint();
      if (!value) {
        return Err(NS_ERROR_UNEXPECTED);
      }
      return Capsule::WebTransportStreamsBlocked(*value, false);
    }
    case CapsuleType::DATAGRAM: {
      nsTArray<uint8_t> payload(aDecoder.GetRemaining());
      return Capsule::WebTransportDatagram(std::move(payload));
    }
    default:
      break;
  }

  return Capsule::Unknown(static_cast<uint64_t>(aType),
                          nsTArray<uint8_t>(aDecoder.GetRemaining()));
}

}  // namespace mozilla::net