Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

5 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # encodeurl
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Encode a URL to a percent-encoded form, excluding already-encoded sequences
  8. ## Installation
  9. This is a [Node.js](https://nodejs.org/en/) module available through the
  10. [npm registry](https://www.npmjs.com/). Installation is done using the
  11. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  12. ```sh
  13. $ npm install encodeurl
  14. ```
  15. ## API
  16. ```js
  17. var encodeUrl = require('encodeurl')
  18. ```
  19. ### encodeUrl(url)
  20. Encode a URL to a percent-encoded form, excluding already-encoded sequences.
  21. This function will take an already-encoded URL and encode all the non-URL
  22. code points (as UTF-8 byte sequences). This function will not encode the
  23. "%" character unless it is not part of a valid sequence (`%20` will be
  24. left as-is, but `%foo` will be encoded as `%25foo`).
  25. This encode is meant to be "safe" and does not throw errors. It will try as
  26. hard as it can to properly encode the given URL, including replacing any raw,
  27. unpaired surrogate pairs with the Unicode replacement character prior to
  28. encoding.
  29. This function is _similar_ to the intrinsic function `encodeURI`, except it
  30. will not encode the `%` character if that is part of a valid sequence, will
  31. not encode `[` and `]` (for IPv6 hostnames) and will replace raw, unpaired
  32. surrogate pairs with the Unicode replacement character (instead of throwing).
  33. ## Examples
  34. ### Encode a URL containing user-controled data
  35. ```js
  36. var encodeUrl = require('encodeurl')
  37. var escapeHtml = require('escape-html')
  38. http.createServer(function onRequest (req, res) {
  39. // get encoded form of inbound url
  40. var url = encodeUrl(req.url)
  41. // create html message
  42. var body = '<p>Location ' + escapeHtml(url) + ' not found</p>'
  43. // send a 404
  44. res.statusCode = 404
  45. res.setHeader('Content-Type', 'text/html; charset=UTF-8')
  46. res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
  47. res.end(body, 'utf-8')
  48. })
  49. ```
  50. ### Encode a URL for use in a header field
  51. ```js
  52. var encodeUrl = require('encodeurl')
  53. var escapeHtml = require('escape-html')
  54. var url = require('url')
  55. http.createServer(function onRequest (req, res) {
  56. // parse inbound url
  57. var href = url.parse(req)
  58. // set new host for redirect
  59. href.host = 'localhost'
  60. href.protocol = 'https:'
  61. href.slashes = true
  62. // create location header
  63. var location = encodeUrl(url.format(href))
  64. // create html message
  65. var body = '<p>Redirecting to new site: ' + escapeHtml(location) + '</p>'
  66. // send a 301
  67. res.statusCode = 301
  68. res.setHeader('Content-Type', 'text/html; charset=UTF-8')
  69. res.setHeader('Content-Length', String(Buffer.byteLength(body, 'utf-8')))
  70. res.setHeader('Location', location)
  71. res.end(body, 'utf-8')
  72. })
  73. ```
  74. ## Testing
  75. ```sh
  76. $ npm test
  77. $ npm run lint
  78. ```
  79. ## References
  80. - [RFC 3986: Uniform Resource Identifier (URI): Generic Syntax][rfc-3986]
  81. - [WHATWG URL Living Standard][whatwg-url]
  82. [rfc-3986]: https://tools.ietf.org/html/rfc3986
  83. [whatwg-url]: https://url.spec.whatwg.org/
  84. ## License
  85. [MIT](LICENSE)
  86. [npm-image]: https://img.shields.io/npm/v/encodeurl.svg
  87. [npm-url]: https://npmjs.org/package/encodeurl
  88. [node-version-image]: https://img.shields.io/node/v/encodeurl.svg
  89. [node-version-url]: https://nodejs.org/en/download
  90. [travis-image]: https://img.shields.io/travis/pillarjs/encodeurl.svg
  91. [travis-url]: https://travis-ci.org/pillarjs/encodeurl
  92. [coveralls-image]: https://img.shields.io/coveralls/pillarjs/encodeurl.svg
  93. [coveralls-url]: https://coveralls.io/r/pillarjs/encodeurl?branch=master
  94. [downloads-image]: https://img.shields.io/npm/dm/encodeurl.svg
  95. [downloads-url]: https://npmjs.org/package/encodeurl