File size: 21,315 Bytes
7b715bc | 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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | /*
Copyright 2013 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using Google.Apis.Http;
using Google.Apis.Services;
using Google.Apis.Testing;
namespace Google.Apis.Requests
{
/// <summary>
/// A batch request which represents individual requests to Google servers. You should add a single service
/// request using the <see cref="Queue"/> method and execute all individual requests using
/// <see cref="ExecuteAsync()"/>. More information about the batch protocol is available in
/// https://developers.google.com/storage/docs/json_api/v1/how-tos/batch.
/// <remarks>
/// Current implementation doesn't retry on unsuccessful individual response and doesn't support requests with
/// different access tokens (different users or scopes).
/// </remarks>
/// </summary>
public sealed class BatchRequest
{
private const string DefaultBatchUrl = "https://www.googleapis.com/batch";
private const int QueueLimit = 1000;
private readonly IList<InnerRequest> allRequests = new List<InnerRequest>();
private readonly string batchUrl;
private readonly IClientService service;
// For testing
internal string BatchUrl => batchUrl;
/// <summary>A concrete type callback for an individual response.</summary>
/// <typeparam name="TResponse">The response type.</typeparam>
/// <param name="content">The parsed content response or <c>null</c> if the request failed or the response
/// could not be parsed using the associated <see cref="IClientService.Serializer"/>.</param>
/// <param name="error">Error or <c>null</c> if the request succeeded and response content was parsed succesfully.</param>
/// <param name="index">The request index.</param>
/// <param name="message">The HTTP individual response.</param>
public delegate void OnResponse<in TResponse>
(TResponse content, RequestError error, int index, HttpResponseMessage message) where TResponse : class;
#region Inner Request
/// <summary>This inner class represents an individual inner request.</summary>
private class InnerRequest
{
/// <summary>Gets or sets the client service request.</summary>
public IClientServiceRequest ClientRequest { get; set; }
/// <summary>Gets or sets the response class type.</summary>
public Type ResponseType { get; set; }
/// <summary>A callback method which will be called after an individual response was parsed.</summary>
/// <param name="content">The parsed content response or <c>null</c> if the request failed or the response
/// could not be parsed using the associated <see cref="IClientService.Serializer"/>.</param>
/// <param name="error">Error or <c>null</c> if the request succeeded and response content was parsed succesfully.</param>
/// <param name="index">The request index.</param>
/// <param name="message">The HTTP individual response.</param>
public virtual void OnResponse(object content, RequestError error, int index, HttpResponseMessage message)
{
// Set ETag on the response.
var eTagValue = message.Headers.ETag != null ? message.Headers.ETag.Tag : null;
var eTagContainer = content as IDirectResponseSchema;
if (eTagContainer != null && eTagContainer.ETag == null && eTagValue != null)
{
eTagContainer.ETag = eTagValue;
}
}
}
/// <summary>
/// This generic inner class represents an individual inner request with a generic response type.
/// </summary>
private class InnerRequest<TResponse> : InnerRequest
where TResponse : class
{
/// <summary>Gets or sets a concrete type callback for an individual response. </summary>
public OnResponse<TResponse> OnResponseCallback { get; set; }
public override void OnResponse(object content, RequestError error, int index,
HttpResponseMessage message)
{
base.OnResponse(content, error, index, message);
if (OnResponseCallback == null)
return;
OnResponseCallback(content as TResponse, error, index, message);
}
}
#endregion
/// <summary>
/// Constructs a new batch request using the given service. See
/// <see cref="BatchRequest(IClientService, string)"/> for more information.
/// </summary>
public BatchRequest(IClientService service)
: this(service, (service as BaseClientService)?.BatchUri ?? DefaultBatchUrl) { }
/// <summary>
/// Constructs a new batch request using the given service. The service's HTTP client is used to create a
/// request to the given server URL and its serializer members are used to serialize the request and
/// deserialize the response.
/// </summary>
public BatchRequest(IClientService service, string batchUrl)
{
this.batchUrl = batchUrl;
this.service = service;
}
/// <summary>Gets the count of all queued requests.</summary>
public int Count => allRequests.Count;
/// <summary>Queues an individual request.</summary>
/// <typeparam name="TResponse">The response's type.</typeparam>
/// <param name="request">The individual request.</param>
/// <param name="callback">A callback which will be called after a response was parsed.</param>
public void Queue<TResponse>(IClientServiceRequest request, OnResponse<TResponse> callback)
where TResponse : class
{
if (Count > QueueLimit)
{
throw new InvalidOperationException("A batch request cannot contain more than 1000 single requests");
}
allRequests.Add(new InnerRequest<TResponse>
{
ClientRequest = request,
ResponseType = typeof(TResponse),
OnResponseCallback = callback,
});
}
/// <summary>Asynchronously executes the batch request.</summary>
public Task ExecuteAsync()
{
return ExecuteAsync(CancellationToken.None);
}
/// <summary>Asynchronously executes the batch request.</summary>
/// <param name="cancellationToken">Cancellation token to cancel operation.</param>
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
if (Count == 0)
{
return;
}
ConfigurableHttpClient httpClient = service.HttpClient;
var requests = from r in allRequests
select r.ClientRequest;
HttpContent outerContent = await CreateOuterRequestContent(requests).ConfigureAwait(false);
string fullContent;
string boundary;
using (var result = await httpClient.PostAsync(new Uri(batchUrl), outerContent, cancellationToken).ConfigureAwait(false))
{
// Will throw as meaningful an exception as possible if there was an error.
await EnsureSuccessAsync(result).ConfigureAwait(false);
// Get the boundary separator.
const string boundaryKey = "boundary=";
var contentType = result.Content.Headers.GetValues("Content-Type").First();
boundary = contentType.Substring(contentType.IndexOf(boundaryKey, StringComparison.Ordinal) + boundaryKey.Length);
fullContent = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
}
int requestIndex = 0;
// While there is still content to read, parse the current HTTP response.
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var startIndex = fullContent.IndexOf("--" + boundary, StringComparison.Ordinal);
if (startIndex == -1)
{
break;
}
fullContent = fullContent.Substring(startIndex + boundary.Length + 2);
var endIndex = fullContent.IndexOf("--" + boundary, StringComparison.Ordinal);
if (endIndex == -1)
{
break;
}
HttpResponseMessage responseMessage = ParseAsHttpResponse(fullContent.Substring(0, endIndex));
if (responseMessage.IsSuccessStatusCode)
{
// Parse the current content object.
var responseContent = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
object deserializedContent = null;
RequestError error = null;
try
{
deserializedContent = service.Serializer.Deserialize(responseContent,
allRequests[requestIndex].ResponseType);
}
catch (Exception ex)
{
error = new RequestError
{
Message = $"The response was read but could not be deserialized using the {nameof(service.Serializer)}.{Environment.NewLine}" +
$"The exception thrown on deserializaton was:{Environment.NewLine}" +
$"{ex}",
};
}
allRequests[requestIndex].OnResponse(deserializedContent, error, requestIndex, responseMessage);
}
else
{
RequestError error;
try
{
// Parse the error from the current response.
error = await service.DeserializeError(responseMessage).ConfigureAwait(false);
}
catch (GoogleApiException ex) when (ex.Error is object)
{
error = ex.Error;
}
allRequests[requestIndex].OnResponse(null, error, requestIndex, responseMessage);
}
requestIndex++;
fullContent = fullContent.Substring(endIndex);
}
}
private async Task EnsureSuccessAsync(HttpResponseMessage result)
{
if (!result.IsSuccessStatusCode)
{
Exception innerException;
try
{
// Try to parse the error from the current response.
RequestError error = await service.DeserializeError(result).ConfigureAwait(false);
// If DeserializeError didn't threw, then we got an error object.
// We wrap it in a GoogleApiException and throw that to use as the inner exception.
// We throw here instead of simply creating a new GoogleApiException
// so as to get the StackTrace.
throw new GoogleApiException(service.Name)
{
Error = error,
HttpStatusCode = result.StatusCode
};
}
catch (Exception ex)
{
// If for any reason we couldn't parse the error,
// or if we did parse it and threw it wrapped in a GoogleApiException,
// let's keep the exception so we can use it as the inner exception
// of the final HttpRequestException
innerException = ex;
}
try
{
// Now that we may have more error detail, let's call EnsureSuccessStatusCode.
// We don't want to introduce breaking changes for users that relied on
// HttpRequestException before, and importantly on its message format which is the only
// way they could access the HttpStatusCode (pre .NET 5).
result.EnsureSuccessStatusCode();
}
// innerException is never null, either it's the one thrown by DeserializeError
// or is the one thrown by us that wraps the deserialized error object.
catch (HttpRequestException original)
{
throw new HttpRequestException(original.Message, innerException);
}
}
}
/// <summary>Parses the given string content to a HTTP response.</summary>
[VisibleForTestOnly]
internal static HttpResponseMessage ParseAsHttpResponse(string content)
{
var response = new HttpResponseMessage();
using (var reader = new StringReader(content))
{
string line = reader.ReadLine();
// Extract empty lines.
while (string.IsNullOrEmpty(line))
{
line = reader.ReadLine();
}
// Extract the outer header.
while (!string.IsNullOrEmpty(line))
{
line = reader.ReadLine();
}
// Extract the status code.
line = reader.ReadLine();
while (string.IsNullOrEmpty(line))
{
line = reader.ReadLine();
}
int code = int.Parse(line.Split(' ')[1]);
response.StatusCode = (HttpStatusCode)code;
// Extract the headers.
IDictionary<string, string> headersDic = new Dictionary<string, string>();
while (!string.IsNullOrEmpty((line = reader.ReadLine())))
{
var separatorIndex = line.IndexOf(':');
var key = line.Substring(0, separatorIndex).Trim();
var value = line.Substring(separatorIndex + 1).Trim();
// Check if the header already exists, and if so append its value
// to the existing value. Fixes issue #548.
if (headersDic.ContainsKey(key))
{
headersDic[key] = headersDic[key] + ", " + value;
}
else
{
headersDic.Add(key, value);
}
}
// Set the content.
string mediaType = null;
if (headersDic.ContainsKey("Content-Type"))
{
mediaType = headersDic["Content-Type"].Split(';', ' ')[0];
headersDic.Remove("Content-Type");
}
string contentBody = reader.ReadToEnd();
// In .NET 6+, the default HttpResponseMessage.Content is an EmptyContent, which is fine
// - we don't need to change it if we don't have any content. In earlier versions,
// the Content property is null by default, but historically we've always populated it.
// We don't want to break users, so we want to continue to make sure it's never null.
if (contentBody != "" || !string.IsNullOrEmpty(mediaType) || response.Content is null)
{
// As of .NET 7, the StringContent constructor fails with a null or empty
// media type. If there's no actual content, we can leave the HttpResponseMessage.Content
// at its default empty value; but if we've got content but no media type,
// we want to pass that to the callback. This would be a very odd situation, but
// text/plain is a reasonable "default media type" here.
if (string.IsNullOrEmpty(mediaType))
{
mediaType = "text/plain";
}
response.Content = new StringContent(contentBody, Encoding.UTF8, mediaType);
}
// Add the headers to the response.
foreach (var keyValue in headersDic)
{
HttpHeaders headers = response.Headers;
// Check if we need to add the current header to the content headers.
if (typeof(HttpContentHeaders).GetProperty(keyValue.Key.Replace("-", "")) != null)
{
headers = response.Content.Headers;
}
// Use TryAddWithoutValidation rather than Add because Mono's validation is
// improperly strict. https://bugzilla.xamarin.com/show_bug.cgi?id=39569
if (!headers.TryAddWithoutValidation(keyValue.Key, keyValue.Value))
{
throw new FormatException($"Could not parse header {keyValue.Key} from batch reply");
}
}
// TODO(peleyal): ContentLength header is x while the "real" content that we read from the stream is
// Content.ReadStringAsAsync().Length is x+2
}
return response;
}
/// <summary>
/// Creates the batch outer request content which includes all the individual requests to Google servers.
/// </summary>
[VisibleForTestOnly]
internal async static Task<HttpContent> CreateOuterRequestContent(IEnumerable<IClientServiceRequest> requests)
{
var mixedContent = new MultipartContent("mixed");
foreach (var request in requests)
{
mixedContent.Add(await CreateIndividualRequest(request).ConfigureAwait(false));
}
// Note: Batch request currently doesn't support GZip.
return mixedContent;
}
/// <summary>Creates the individual server request.</summary>
[VisibleForTestOnly]
internal static async Task<HttpContent> CreateIndividualRequest(IClientServiceRequest request)
{
HttpRequestMessage requestMessage = request.CreateRequest(false);
string requestContent = await CreateRequestContentString(requestMessage).ConfigureAwait(false);
var content = new StringContent(requestContent);
content.Headers.ContentType = new MediaTypeHeaderValue("application/http");
return content;
}
/// <summary>
/// Creates a string representation that includes the request's headers and content based on the input HTTP
/// request message.
/// </summary>
[VisibleForTestOnly]
internal static async Task<string> CreateRequestContentString(HttpRequestMessage requestMessage)
{
var sb = new StringBuilder();
sb.AppendFormat("{0} {1}", requestMessage.Method, requestMessage.RequestUri.AbsoluteUri);
// Add Headers.
foreach (var otherHeader in requestMessage.Headers)
{
sb.Append(Environment.NewLine)
.AppendFormat("{0}: {1}", otherHeader.Key, string.Join(", ", otherHeader.Value.ToArray()));
}
// Add content headers.
if (requestMessage.Content != null)
{
foreach (var contentHeader in requestMessage.Content.Headers)
{
sb.Append(Environment.NewLine)
.AppendFormat("{0}: {1}", contentHeader.Key, string.Join(", ", contentHeader.Value.ToArray()));
}
}
// Content.
if (requestMessage.Content != null)
{
sb.Append(Environment.NewLine);
var content = await requestMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
sb.Append("Content-Length: ").Append(content.Length);
sb.Append(Environment.NewLine).Append(Environment.NewLine).Append(content);
}
return sb.Append(Environment.NewLine).ToString();
}
}
} |