Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

urlencoded.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. const { utf8Encode, utf8DecodeWithoutBOM } = require("./encoding");
  3. const { percentDecodeBytes, utf8PercentEncodeString, isURLEncodedPercentEncode } = require("./percent-encoding");
  4. // https://url.spec.whatwg.org/#concept-urlencoded-parser
  5. function parseUrlencoded(input) {
  6. const sequences = strictlySplitByteSequence(input, 38);
  7. const output = [];
  8. for (const bytes of sequences) {
  9. if (bytes.length === 0) {
  10. continue;
  11. }
  12. let name, value;
  13. const indexOfEqual = bytes.indexOf(61);
  14. if (indexOfEqual >= 0) {
  15. name = bytes.slice(0, indexOfEqual);
  16. value = bytes.slice(indexOfEqual + 1);
  17. } else {
  18. name = bytes;
  19. value = new Uint8Array(0);
  20. }
  21. name = replaceByteInByteSequence(name, 0x2B, 0x20);
  22. value = replaceByteInByteSequence(value, 0x2B, 0x20);
  23. const nameString = utf8DecodeWithoutBOM(percentDecodeBytes(name));
  24. const valueString = utf8DecodeWithoutBOM(percentDecodeBytes(value));
  25. output.push([nameString, valueString]);
  26. }
  27. return output;
  28. }
  29. // https://url.spec.whatwg.org/#concept-urlencoded-string-parser
  30. function parseUrlencodedString(input) {
  31. return parseUrlencoded(utf8Encode(input));
  32. }
  33. // https://url.spec.whatwg.org/#concept-urlencoded-serializer
  34. function serializeUrlencoded(tuples, encodingOverride = undefined) {
  35. let encoding = "utf-8";
  36. if (encodingOverride !== undefined) {
  37. // TODO "get the output encoding", i.e. handle encoding labels vs. names.
  38. encoding = encodingOverride;
  39. }
  40. let output = "";
  41. for (const [i, tuple] of tuples.entries()) {
  42. // TODO: handle encoding override
  43. const name = utf8PercentEncodeString(tuple[0], isURLEncodedPercentEncode, true);
  44. let value = tuple[1];
  45. if (tuple.length > 2 && tuple[2] !== undefined) {
  46. if (tuple[2] === "hidden" && name === "_charset_") {
  47. value = encoding;
  48. } else if (tuple[2] === "file") {
  49. // value is a File object
  50. value = value.name;
  51. }
  52. }
  53. value = utf8PercentEncodeString(value, isURLEncodedPercentEncode, true);
  54. if (i !== 0) {
  55. output += "&";
  56. }
  57. output += `${name}=${value}`;
  58. }
  59. return output;
  60. }
  61. function strictlySplitByteSequence(buf, cp) {
  62. const list = [];
  63. let last = 0;
  64. let i = buf.indexOf(cp);
  65. while (i >= 0) {
  66. list.push(buf.slice(last, i));
  67. last = i + 1;
  68. i = buf.indexOf(cp, last);
  69. }
  70. if (last !== buf.length) {
  71. list.push(buf.slice(last));
  72. }
  73. return list;
  74. }
  75. function replaceByteInByteSequence(buf, from, to) {
  76. let i = buf.indexOf(from);
  77. while (i >= 0) {
  78. buf[i] = to;
  79. i = buf.indexOf(from, i + 1);
  80. }
  81. return buf;
  82. }
  83. module.exports = {
  84. parseUrlencodedString,
  85. serializeUrlencoded
  86. };