Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
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.

read.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*!
  2. * body-parser
  3. * Copyright(c) 2014 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var getBody = require('raw-body')
  10. var iconv = require('iconv-lite')
  11. var onFinished = require('on-finished')
  12. var typer = require('media-typer')
  13. var zlib = require('zlib')
  14. /**
  15. * Module exports.
  16. */
  17. module.exports = read
  18. /**
  19. * Read a request into a buffer and parse.
  20. *
  21. * @param {object} req
  22. * @param {object} res
  23. * @param {function} next
  24. * @param {function} parse
  25. * @param {object} options
  26. * @api private
  27. */
  28. function read(req, res, next, parse, options) {
  29. var length
  30. var stream
  31. // flag as parsed
  32. req._body = true
  33. try {
  34. stream = contentstream(req, options.inflate)
  35. length = stream.length
  36. delete stream.length
  37. } catch (err) {
  38. return next(err)
  39. }
  40. options = options || {}
  41. options.length = length
  42. var encoding = options.encoding !== null
  43. ? options.encoding || 'utf-8'
  44. : null
  45. var verify = options.verify
  46. options.encoding = verify
  47. ? null
  48. : encoding
  49. // read body
  50. getBody(stream, options, function (err, body) {
  51. if (err) {
  52. if (!err.status) {
  53. err.status = 400
  54. }
  55. // read off entire request
  56. stream.resume()
  57. onFinished(req, function onfinished() {
  58. next(err)
  59. })
  60. return
  61. }
  62. // verify
  63. if (verify) {
  64. try {
  65. verify(req, res, body, encoding)
  66. } catch (err) {
  67. if (!err.status) err.status = 403
  68. return next(err)
  69. }
  70. }
  71. // parse
  72. try {
  73. body = typeof body !== 'string' && encoding !== null
  74. ? iconv.decode(body, encoding)
  75. : body
  76. req.body = parse(body)
  77. } catch (err) {
  78. if (!err.status) {
  79. err.body = body
  80. err.status = 400
  81. }
  82. return next(err)
  83. }
  84. next()
  85. })
  86. }
  87. /**
  88. * Get the content stream of the request.
  89. *
  90. * @param {object} req
  91. * @param {boolean} [inflate=true]
  92. * @return {object}
  93. * @api private
  94. */
  95. function contentstream(req, inflate) {
  96. var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
  97. var err
  98. var length = req.headers['content-length']
  99. var stream
  100. if (inflate === false && encoding !== 'identity') {
  101. err = new Error('content encoding unsupported')
  102. err.status = 415
  103. throw err
  104. }
  105. switch (encoding) {
  106. case 'deflate':
  107. stream = zlib.createInflate()
  108. req.pipe(stream)
  109. break
  110. case 'gzip':
  111. stream = zlib.createGunzip()
  112. req.pipe(stream)
  113. break
  114. case 'identity':
  115. stream = req
  116. stream.length = length
  117. break
  118. default:
  119. err = new Error('unsupported content encoding')
  120. err.status = 415
  121. throw err
  122. }
  123. return stream
  124. }