RequestParser.coffee 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. module.exports = RequestParser =
  2. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  3. MAX_TIMEOUT: 300
  4. parse: (body, callback = (error, data) ->) ->
  5. response = {}
  6. if !body.compile?
  7. return callback "top level object should have a compile attribute"
  8. compile = body.compile
  9. compile.options ||= {}
  10. try
  11. response.compiler = @_parseAttribute "compiler",
  12. compile.options.compiler,
  13. validValues: @VALID_COMPILERS
  14. default: "pdflatex"
  15. type: "string"
  16. response.timeout = @_parseAttribute "timeout",
  17. compile.options.timeout
  18. default: RequestParser.MAX_TIMEOUT
  19. type: "number"
  20. response.imageName = @_parseAttribute "imageName",
  21. compile.options.imageName,
  22. type: "string"
  23. response.draft = @_parseAttribute "draft",
  24. compile.options.draft,
  25. default: false,
  26. type: "boolean"
  27. if response.timeout > RequestParser.MAX_TIMEOUT
  28. response.timeout = RequestParser.MAX_TIMEOUT
  29. response.timeout = response.timeout * 1000 # milliseconds
  30. response.resources = (@_parseResource(resource) for resource in (compile.resources or []))
  31. rootResourcePath = @_parseAttribute "rootResourcePath",
  32. compile.rootResourcePath
  33. default: "main.tex"
  34. type: "string"
  35. response.rootResourcePath = RequestParser._sanitizePath(rootResourcePath)
  36. catch error
  37. return callback error
  38. callback null, response
  39. _parseResource: (resource) ->
  40. if !resource.path? or typeof resource.path != "string"
  41. throw "all resources should have a path attribute"
  42. if resource.modified?
  43. modified = new Date(resource.modified)
  44. if isNaN(modified.getTime())
  45. throw "resource modified date could not be understood: #{resource.modified}"
  46. if !resource.url? and !resource.content?
  47. throw "all resources should have either a url or content attribute"
  48. if resource.content? and typeof resource.content != "string"
  49. throw "content attribute should be a string"
  50. if resource.url? and typeof resource.url != "string"
  51. throw "url attribute should be a string"
  52. return {
  53. path: resource.path
  54. modified: modified
  55. url: resource.url
  56. content: resource.content
  57. }
  58. _parseAttribute: (name, attribute, options) ->
  59. if attribute?
  60. if options.validValues?
  61. if options.validValues.indexOf(attribute) == -1
  62. throw "#{name} attribute should be one of: #{options.validValues.join(", ")}"
  63. if options.type?
  64. if typeof attribute != options.type
  65. throw "#{name} attribute should be a #{options.type}"
  66. else
  67. return options.default if options.default?
  68. return attribute
  69. _sanitizePath: (path) ->
  70. # See http://php.net/manual/en/function.escapeshellcmd.php
  71. path.replace(/[\#\&\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\x0A\xFF\x00]/g, "")