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

content / test / data / service_worker / file_upload_worker.js [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Service worker for the file upload test. It responds to a POST submission
// with an HTML document whose body describes the received form data in JSON.

function describeValue(value) {
  const result = {};
  if (value instanceof File) {
    return {type: 'file', size: value.size, name: value.name};
  } else if (value instanceof Blob) {
    return {type: 'blob', size: value.size};
  } else {
    return {type: 'string', data: value};
  }
}

function extractBoundary(request) {
  const reg = new RegExp('multipart\/form-data; boundary=(.*)');
  for (var header of request.headers) {
    if (header[0] == 'content-type') {
      var regResult = reg.exec(header[1]);
      if (regResult)
        return regResult[1];
    }
  }
  return undefined;
}

async function asFormData(request) {
  const formData = await request.formData();
  const result = {};
  result.entries = [];
  for (var pair of formData.entries()) {
    result.entries.push({key: pair[0], value: describeValue(pair[1])});
  }
  return JSON.stringify(result);
}

async function asText(request) {
  const boundary = extractBoundary(request);
  if (!boundary)
    throw 'error: no boundary found';

  const text = await request.text();
  return JSON.stringify({
    boundary: boundary,
    body: text
  });
}

async function asBlob(request) {
  const boundary = extractBoundary(request);
  if (!boundary)
    throw 'error: no boundary found';

  const blob = await request.blob();
  return JSON.stringify({
    boundary: boundary,
    bodySize: blob.size
  });
}

async function generateResponse(request, getAs) {
  let resultString;
  if (getAs == 'formData')
    resultString = await asFormData(request);
  else if (getAs == 'blob')
    resultString = await asBlob(request);
  else
    resultString = await asText(request);

  const body = String.raw`
    <!doctype html>
    <html>
    <title>form submitted</title>
    <body>${resultString}</body>
    </html>
  `;
  const headers = {'content-type': 'text/html'};
  return new Response(body, {headers});
}

self.addEventListener('fetch', event => {
  if (event.request.method != 'POST')
    return;
  const url = new URL(event.request.url);
  const getAs = url.searchParams.get('getAs');
  if (getAs == 'fallback')
    return;
  event.respondWith(generateResponse(event.request, getAs));
});