RequestParser.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. # The syncType specifies whether the request contains all
  31. # resources (full) or only those resources to be updated
  32. # in-place (incremental).
  33. response.syncType = @_parseAttribute "syncType",
  34. compile.options.syncType,
  35. validValues: ["full", "incremental"]
  36. type: "string"
  37. # The syncState is an identifier passed in with the request
  38. # which has the property that it changes when any resource is
  39. # added, deleted, moved or renamed.
  40. #
  41. # on syncType full the syncState identifier is passed in and
  42. # stored
  43. #
  44. # on syncType incremental the syncState identifier must match
  45. # the stored value
  46. response.syncState = @_parseAttribute "syncState",
  47. compile.options.syncState,
  48. type: "string"
  49. if response.timeout > RequestParser.MAX_TIMEOUT
  50. response.timeout = RequestParser.MAX_TIMEOUT
  51. response.timeout = response.timeout * 1000 # milliseconds
  52. response.resources = (@_parseResource(resource) for resource in (compile.resources or []))
  53. rootResourcePath = @_parseAttribute "rootResourcePath",
  54. compile.rootResourcePath
  55. default: "main.tex"
  56. type: "string"
  57. originalRootResourcePath = rootResourcePath
  58. sanitizedRootResourcePath = RequestParser._sanitizePath(rootResourcePath)
  59. response.rootResourcePath = RequestParser._checkPath(sanitizedRootResourcePath)
  60. for resource in response.resources
  61. if resource.path == originalRootResourcePath
  62. resource.path = sanitizedRootResourcePath
  63. catch error
  64. return callback error
  65. callback null, response
  66. _parseResource: (resource) ->
  67. if !resource.path? or typeof resource.path != "string"
  68. throw "all resources should have a path attribute"
  69. if resource.modified?
  70. modified = new Date(resource.modified)
  71. if isNaN(modified.getTime())
  72. throw "resource modified date could not be understood: #{resource.modified}"
  73. if !resource.url? and !resource.content?
  74. throw "all resources should have either a url or content attribute"
  75. if resource.content? and typeof resource.content != "string"
  76. throw "content attribute should be a string"
  77. if resource.url? and typeof resource.url != "string"
  78. throw "url attribute should be a string"
  79. return {
  80. path: resource.path
  81. modified: modified
  82. url: resource.url
  83. content: resource.content
  84. }
  85. _parseAttribute: (name, attribute, options) ->
  86. if attribute?
  87. if options.validValues?
  88. if options.validValues.indexOf(attribute) == -1
  89. throw "#{name} attribute should be one of: #{options.validValues.join(", ")}"
  90. if options.type?
  91. if typeof attribute != options.type
  92. throw "#{name} attribute should be a #{options.type}"
  93. else
  94. return options.default if options.default?
  95. return attribute
  96. _sanitizePath: (path) ->
  97. # See http://php.net/manual/en/function.escapeshellcmd.php
  98. path.replace(/[\#\&\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\x0A\xFF\x00]/g, "")
  99. _checkPath: (path) ->
  100. # check that the request does not use a relative path
  101. for dir in path.split('/')
  102. if dir == '..'
  103. throw "relative path in root resource"
  104. return path