PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/runtime/base/execution_context.h

https://github.com/tmjnaid/hiphop-php
C Header | 303 lines | 199 code | 45 blank | 59 comment | 1 complexity | 8ad4eab8ee340cfb1d2fdfde3f5b136e MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef __HPHP_EXECUTION_CONTEXT_H__
  17. #define __HPHP_EXECUTION_CONTEXT_H__
  18. #include <runtime/base/complex_types.h>
  19. #include <runtime/base/server/transport.h>
  20. #include <runtime/base/resource_data.h>
  21. #include <runtime/base/fiber_safe.h>
  22. #include <runtime/base/debuggable.h>
  23. #include <runtime/base/util/string_buffer.h>
  24. #include <util/thread_local.h>
  25. namespace HPHP {
  26. ///////////////////////////////////////////////////////////////////////////////
  27. /**
  28. * Mainly designed for extensions to perform initialization and shutdown
  29. * sequences at request scope.
  30. */
  31. class RequestEventHandler {
  32. public:
  33. RequestEventHandler() : m_inited(false) {}
  34. virtual ~RequestEventHandler() {}
  35. virtual void requestInit() = 0;
  36. virtual void requestShutdown() = 0;
  37. void setInited(bool inited) { m_inited = inited;}
  38. bool getInited() const { return m_inited;}
  39. // Priority of request shutdown call. Lower priority value means
  40. // requestShutdown is called earlier than higher priority values.
  41. virtual int priority() const { return 0;}
  42. protected:
  43. bool m_inited;
  44. };
  45. ///////////////////////////////////////////////////////////////////////////////
  46. /**
  47. * Put all global variables here so we can gather them into one thread-local
  48. * variable for easy access.
  49. */
  50. class ExecutionContext : public FiberLocal, public IDebuggable {
  51. public:
  52. enum ShutdownType {
  53. ShutDown,
  54. PostSend,
  55. CleanUp,
  56. ShutdownTypeCount
  57. };
  58. enum ErrorThrowMode {
  59. NeverThrow,
  60. ThrowIfUnhandled,
  61. AlwaysThrow,
  62. };
  63. enum ErrorState {
  64. NoError,
  65. ErrorRaised,
  66. ExecutingUserHandler,
  67. ErrorRaisedByUserHandler,
  68. };
  69. public:
  70. ExecutionContext();
  71. ~ExecutionContext();
  72. // For RPCRequestHandler
  73. void backupSession();
  74. void restoreSession();
  75. // implementing FiberSafe
  76. virtual void fiberInit(FiberLocal *src, FiberReferenceMap &refMap);
  77. virtual void fiberExit(FiberLocal *src, FiberReferenceMap &refMap);
  78. // implementing IDebuggable
  79. virtual void debuggerInfo(InfoVec &info);
  80. /**
  81. * System settings.
  82. */
  83. Transport *getTransport() { return m_transport;}
  84. void setTransport(Transport *transport) { m_transport = transport;}
  85. String getMimeType() const;
  86. void setContentType(CStrRef mimetype, CStrRef charset);
  87. int64 getRequestMemoryMaxBytes() const { return m_maxMemory; }
  88. void setRequestMemoryMaxBytes(int64 max);
  89. int64 getRequestTimeLimit() const { return m_maxTime; }
  90. void setRequestTimeLimit(int64 limit) { m_maxTime = limit;}
  91. String getCwd() const { return m_cwd;}
  92. void setCwd(CStrRef cwd) { m_cwd = cwd;}
  93. /**
  94. * Write to output.
  95. */
  96. void write(CStrRef s);
  97. void write(const char *s, int len);
  98. void write(const char *s) { write(s, strlen(s));}
  99. void writeStdout(const char *s, int len);
  100. typedef void (*PFUNC_STDOUT)(const char *s, int len, void *data);
  101. void setStdout(PFUNC_STDOUT func, void *data);
  102. /**
  103. * Output buffering.
  104. */
  105. void obStart(CVarRef handler = null);
  106. String obCopyContents();
  107. String obDetachContents();
  108. int obGetContentLength();
  109. void obClean();
  110. bool obFlush();
  111. void obFlushAll();
  112. bool obEnd();
  113. void obEndAll();
  114. int obGetLevel();
  115. Array obGetStatus(bool full);
  116. void obSetImplicitFlush(bool on);
  117. Array obGetHandlers();
  118. void obProtect(bool on); // making sure obEnd() never passes current level
  119. void flush();
  120. /**
  121. * Request sequences and program execution hooks.
  122. */
  123. void registerRequestEventHandler(RequestEventHandler *handler);
  124. void registerShutdownFunction(CVarRef function, Array arguments,
  125. ShutdownType type);
  126. void onRequestShutdown();
  127. void onShutdownPreSend();
  128. void onShutdownPostSend();
  129. void registerTickFunction(CVarRef function, Array arguments);
  130. void unregisterTickFunction(CVarRef function);
  131. void onTick();
  132. Array backupShutdowns() const { return m_shutdowns;}
  133. void restoreShutdowns(CArrRef shutdowns) { m_shutdowns = shutdowns;}
  134. /**
  135. * Error handling
  136. */
  137. Variant pushUserErrorHandler(CVarRef function, int error_types);
  138. Variant pushUserExceptionHandler(CVarRef function);
  139. void popUserErrorHandler();
  140. void popUserExceptionHandler();
  141. bool errorNeedsHandling(int errnum,
  142. bool callUserHandler,
  143. ErrorThrowMode mode);
  144. void handleError(const std::string &msg,
  145. int errnum,
  146. bool callUserHandler,
  147. ErrorThrowMode mode,
  148. const std::string &prefix);
  149. bool callUserErrorHandler(const Exception &e, int errnum,
  150. bool swallowExceptions);
  151. void recordLastError(const Exception &e, int errnum = 0);
  152. bool onFatalError(const Exception &e); // returns handled
  153. void onUnhandledException(Object e);
  154. int getErrorState() const { return m_errorState;}
  155. void setErrorState(int state) { m_errorState = state;}
  156. String getLastError() const { return m_lastError;}
  157. int getLastErrorNumber() const { return m_lastErrorNum;}
  158. int getErrorReportingLevel() const { return m_errorReportingLevel;}
  159. void setErrorReportingLevel(int level) { m_errorReportingLevel = level;}
  160. String getErrorPage() const { return m_errorPage;}
  161. void setErrorPage(CStrRef page) { m_errorPage = page;}
  162. bool getLogErrors() const { return m_logErrors;}
  163. void setLogErrors(bool on);
  164. String getErrorLog() const { return m_errorLog;}
  165. void setErrorLog(CStrRef filename);
  166. /**
  167. * Misc. settings
  168. */
  169. String getenv(CStrRef name) const;
  170. void setenv(CStrRef name, CStrRef value);
  171. String getTimeZone() const { return m_timezone;}
  172. void setTimeZone(CStrRef timezone) { m_timezone = timezone;}
  173. String getDefaultTimeZone() const { return m_timezoneDefault;}
  174. String getArgSeparatorOutput() const {
  175. if (m_argSeparatorOutput.isNull()) return "&";
  176. return m_argSeparatorOutput;
  177. }
  178. void setArgSeparatorOutput(CStrRef s) { m_argSeparatorOutput = s;}
  179. void setThrowAllErrors(bool f) { m_throwAllErrors = f; }
  180. private:
  181. class OutputBuffer {
  182. public:
  183. OutputBuffer() : oss(8192) {}
  184. StringBuffer oss;
  185. Variant handler;
  186. };
  187. // system settings
  188. Transport *m_transport;
  189. int64 m_maxMemory;
  190. int64 m_maxTime;
  191. String m_cwd;
  192. // output buffering
  193. StringBuffer *m_out; // current output buffer
  194. std::list<OutputBuffer*> m_buffers; // a stack of output buffers
  195. bool m_implicitFlush;
  196. int m_protectedLevel;
  197. PFUNC_STDOUT m_stdout;
  198. void *m_stdoutData;
  199. // request handlers
  200. std::set<RequestEventHandler*> m_requestEventHandlerSet;
  201. std::vector<RequestEventHandler*> m_requestEventHandlers;
  202. Array m_shutdowns;
  203. Array m_ticks;
  204. // error handling
  205. std::vector<std::pair<Variant,int> > m_userErrorHandlers;
  206. std::vector<Variant> m_userExceptionHandlers;
  207. int m_errorState;
  208. int m_errorReportingLevel;
  209. String m_lastError;
  210. int m_lastErrorNum;
  211. std::string m_errorPage;
  212. bool m_logErrors;
  213. String m_errorLog;
  214. // misc settings
  215. Array m_envs;
  216. String m_timezone;
  217. String m_timezoneDefault;
  218. String m_argSeparatorOutput;
  219. bool m_throwAllErrors;
  220. // session backup/restore for RPCRequestHandler
  221. Array m_shutdownsBackup;
  222. std::vector<std::pair<Variant,int> > m_userErrorHandlersBackup;
  223. std::vector<Variant> m_userExceptionHandlersBackup;
  224. // helper functions
  225. void resetCurrentBuffer();
  226. void executeFunctions(CArrRef funcs);
  227. };
  228. ///////////////////////////////////////////////////////////////////////////////
  229. class PersistentObjectStore {
  230. public:
  231. ~PersistentObjectStore();
  232. int size() const;
  233. void set(const char *type, const char *name, ResourceData *obj);
  234. ResourceData *get(const char *type, const char *name);
  235. void remove(const char *type, const char *name);
  236. const ResourceMap &getMap(const char *type);
  237. private:
  238. ResourceMapMap m_objects;
  239. void removeObject(ResourceData *data);
  240. };
  241. ///////////////////////////////////////////////////////////////////////////////
  242. class Silencer {
  243. public:
  244. Silencer();
  245. ~Silencer();
  246. void enable();
  247. void disable();
  248. Variant disable(CVarRef v);
  249. private:
  250. bool m_active;
  251. int m_errorReportingValue;
  252. };
  253. ///////////////////////////////////////////////////////////////////////////////
  254. extern DECLARE_THREAD_LOCAL(ExecutionContext, g_context);
  255. extern DECLARE_THREAD_LOCAL(PersistentObjectStore, g_persistentObjects);
  256. ///////////////////////////////////////////////////////////////////////////////
  257. }
  258. #endif // __HPHP_EXECUTION_CONTEXT_H__