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.

index.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. var typer = require('media-typer')
  2. var mime = require('mime-types')
  3. module.exports = typeofrequest;
  4. typeofrequest.is = typeis;
  5. typeofrequest.hasBody = hasbody;
  6. typeofrequest.normalize = normalize;
  7. typeofrequest.match = mimeMatch;
  8. /**
  9. * Compare a `value` content-type with `types`.
  10. * Each `type` can be an extension like `html`,
  11. * a special shortcut like `multipart` or `urlencoded`,
  12. * or a mime type.
  13. *
  14. * If no types match, `false` is returned.
  15. * Otherwise, the first `type` that matches is returned.
  16. *
  17. * @param {String} value
  18. * @param {Array} types
  19. * @return String
  20. */
  21. function typeis(value, types_) {
  22. var i
  23. var types = types_
  24. // remove parameters and normalize
  25. value = typenormalize(value)
  26. // no type or invalid
  27. if (!value) {
  28. return false
  29. }
  30. // support flattened arguments
  31. if (types && !Array.isArray(types)) {
  32. types = new Array(arguments.length - 1)
  33. for (i = 0; i < types.length; i++) {
  34. types[i] = arguments[i + 1]
  35. }
  36. }
  37. // no types, return the content type
  38. if (!types || !types.length) return value;
  39. var type
  40. for (i = 0; i < types.length; i++) {
  41. if (mimeMatch(normalize(type = types[i]), value)) {
  42. return type[0] === '+' || ~type.indexOf('*')
  43. ? value
  44. : type
  45. }
  46. }
  47. // no matches
  48. return false;
  49. }
  50. /**
  51. * Check if a request has a request body.
  52. * A request with a body __must__ either have `transfer-encoding`
  53. * or `content-length` headers set.
  54. * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
  55. *
  56. * @param {Object} request
  57. * @return {Boolean}
  58. * @api public
  59. */
  60. function hasbody(req) {
  61. var headers = req.headers;
  62. if ('transfer-encoding' in headers) return true;
  63. return !isNaN(headers['content-length']);
  64. }
  65. /**
  66. * Check if the incoming request contains the "Content-Type"
  67. * header field, and it contains any of the give mime `type`s.
  68. * If there is no request body, `null` is returned.
  69. * If there is no content type, `false` is returned.
  70. * Otherwise, it returns the first `type` that matches.
  71. *
  72. * Examples:
  73. *
  74. * // With Content-Type: text/html; charset=utf-8
  75. * this.is('html'); // => 'html'
  76. * this.is('text/html'); // => 'text/html'
  77. * this.is('text/*', 'application/json'); // => 'text/html'
  78. *
  79. * // When Content-Type is application/json
  80. * this.is('json', 'urlencoded'); // => 'json'
  81. * this.is('application/json'); // => 'application/json'
  82. * this.is('html', 'application/*'); // => 'application/json'
  83. *
  84. * this.is('html'); // => false
  85. *
  86. * @param {String|Array} types...
  87. * @return {String|false|null}
  88. * @api public
  89. */
  90. function typeofrequest(req, types_) {
  91. var types = types_
  92. // no body
  93. if (!hasbody(req)) {
  94. return null
  95. }
  96. // support flattened arguments
  97. if (arguments.length > 2) {
  98. types = new Array(arguments.length - 1)
  99. for (var i = 0; i < types.length; i++) {
  100. types[i] = arguments[i + 1]
  101. }
  102. }
  103. // request content type
  104. var value = req.headers['content-type']
  105. return typeis(value, types);
  106. }
  107. /**
  108. * Normalize a mime type.
  109. * If it's a shorthand, expand it to a valid mime type.
  110. *
  111. * In general, you probably want:
  112. *
  113. * var type = is(req, ['urlencoded', 'json', 'multipart']);
  114. *
  115. * Then use the appropriate body parsers.
  116. * These three are the most common request body types
  117. * and are thus ensured to work.
  118. *
  119. * @param {String} type
  120. * @api private
  121. */
  122. function normalize(type) {
  123. switch (type) {
  124. case 'urlencoded': return 'application/x-www-form-urlencoded';
  125. case 'multipart':
  126. type = 'multipart/*';
  127. break;
  128. }
  129. return type[0] === '+' || ~type.indexOf('/')
  130. ? type
  131. : mime.lookup(type)
  132. }
  133. /**
  134. * Check if `exected` mime type
  135. * matches `actual` mime type with
  136. * wildcard and +suffix support.
  137. *
  138. * @param {String} expected
  139. * @param {String} actual
  140. * @return {Boolean}
  141. * @api private
  142. */
  143. function mimeMatch(expected, actual) {
  144. // invalid type
  145. if (expected === false) {
  146. return false
  147. }
  148. // exact match
  149. if (expected === actual) {
  150. return true
  151. }
  152. actual = actual.split('/');
  153. if (expected[0] === '+') {
  154. // support +suffix
  155. return Boolean(actual[1])
  156. && expected.length <= actual[1].length
  157. && expected === actual[1].substr(0 - expected.length)
  158. }
  159. if (!~expected.indexOf('*')) return false;
  160. expected = expected.split('/');
  161. if (expected[0] === '*') {
  162. // support */yyy
  163. return expected[1] === actual[1]
  164. }
  165. if (expected[1] === '*') {
  166. // support xxx/*
  167. return expected[0] === actual[0]
  168. }
  169. if (expected[1][0] === '*' && expected[1][1] === '+') {
  170. // support xxx/*+zzz
  171. return expected[0] === actual[0]
  172. && expected[1].length <= actual[1].length + 1
  173. && expected[1].substr(1) === actual[1].substr(1 - expected[1].length)
  174. }
  175. return false
  176. }
  177. /**
  178. * Normalize a type and remove parameters.
  179. *
  180. * @param {string} value
  181. * @return {string}
  182. * @api private
  183. */
  184. function typenormalize(value) {
  185. try {
  186. var type = typer.parse(value)
  187. delete type.parameters
  188. return typer.format(type)
  189. } catch (err) {
  190. return null
  191. }
  192. }