1 /*
2  * SessionMathJax.cpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #include "SessionMathJax.hpp"
17 
18 #include <shared_core/Error.hpp>
19 #include <shared_core/FilePath.hpp>
20 #include <core/Exec.hpp>
21 
22 #include <core/http/Util.hpp>
23 
24 #include <session/SessionModuleContext.hpp>
25 
26 #define kMathJaxURIPrefix "/mathjax/"
27 
28 using namespace rstudio::core;
29 
30 namespace rstudio {
31 namespace session {
32 namespace modules {
33 namespace mathjax {
34 
35 namespace {
36 
handleMathJax(const http::Request & request,http::Response * pResponse)37 void handleMathJax(const http::Request& request, http::Response* pResponse)
38 {
39    // extract path from URI
40    std::string path = request.path().substr(strlen(kMathJaxURIPrefix));
41 
42    // construct path to resource
43    FilePath mathjaxPath = options().mathjaxPath();
44    FilePath resourcePath = mathjaxPath.completePath(path);
45    pResponse->setCacheableFile(resourcePath, request);
46 }
47 
48 } // end anonymous namespace
49 
initialize()50 Error initialize()
51 {
52    // install rpc methods
53    using boost::bind;
54    using namespace module_context;
55    ExecBlock initBlock;
56    initBlock.addFunctions()
57       (bind(registerUriHandler, "/mathjax", handleMathJax))
58    ;
59    return initBlock.execute();
60 }
61 
62 } // end namespace mathjax
63 } // end namespace modules
64 } // end namespace session
65 } // end namespace rstudio
66