RequestParser.coffee 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. response.check = @_parseAttribute "check",
  28. compile.options.check,
  29. type: "string"
  30. if response.timeout > RequestParser.MAX_TIMEOUT
  31. response.timeout = RequestParser.MAX_TIMEOUT
  32. response.timeout = response.timeout * 1000 # milliseconds
  33. response.resources = (@_parseResource(resource) for resource in (compile.resources or []))
  34. rootResourcePath = @_parseAttribute "rootResourcePath",
  35. compile.rootResourcePath
  36. default: "main.tex"
  37. type: "string"
  38. response.rootResourcePath = RequestParser._sanitizePath(rootResourcePath)
  39. catch error
  40. return callback error
  41. callback null, response
  42. _parseResource: (resource) ->
  43. if !resource.path? or typeof resource.path != "string"
  44. throw "all resources should have a path attribute"
  45. if resource.modified?
  46. modified = new Date(resource.modified)
  47. if isNaN(modified.getTime())
  48. throw "resource modified date could not be understood: #{resource.modified}"
  49. if !resource.url? and !resource.content?
  50. throw "all resources should have either a url or content attribute"
  51. if resource.content? and typeof resource.content != "string"
  52. throw "content attribute should be a string"
  53. if resource.url? and typeof resource.url != "string"
  54. throw "url attribute should be a string"
  55. return {
  56. path: resource.path
  57. modified: modified
  58. url: resource.url
  59. content: resource.content
  60. }
  61. _parseAttribute: (name, attribute, options) ->
  62. if attribute?
  63. if options.validValues?
  64. if options.validValues.indexOf(attribute) == -1
  65. throw "#{name} attribute should be one of: #{options.validValues.join(", ")}"
  66. if options.type?
  67. if typeof attribute != options.type
  68. throw "#{name} attribute should be a #{options.type}"
  69. else
  70. return options.default if options.default?
  71. return attribute
  72. _sanitizePath: (path) ->
  73. # See http://php.net/manual/en/function.escapeshellcmd.php
  74. path.replace(/[\#\&\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\x0A\xFF\x00]/g, "")