mongoose.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright (c) 2004-2009 Sergey Lyubka
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. # THE SOFTWARE.
  20. #
  21. # $Id: mongoose.py 471 2009-08-30 14:30:21Z valenok $
  22. """
  23. This module provides python binding for the Mongoose web server.
  24. There are two classes defined:
  25. Connection: - wraps all functions that accept struct mg_connection pointer
  26. as first argument.
  27. Mongoose: wraps all functions that accept struct mg_context pointer as
  28. first argument.
  29. Creating Mongoose object automatically starts server, deleting object
  30. automatically stops it. There is no need to call mg_start() or mg_stop().
  31. """
  32. import ctypes
  33. import os
  34. NEW_REQUEST = 0
  35. REQUEST_COMPLETE = 1
  36. HTTP_ERROR = 2
  37. EVENT_LOG = 3
  38. INIT_SSL = 4
  39. WEBSOCKET_CONNECT = 5
  40. WEBSOCKET_READY = 6
  41. WEBSOCKET_MESSAGE = 7
  42. WEBSOCKET_CLOSE = 8
  43. class mg_header(ctypes.Structure):
  44. """A wrapper for struct mg_header."""
  45. _fields_ = [
  46. ('name', ctypes.c_char_p),
  47. ('value', ctypes.c_char_p),
  48. ]
  49. class mg_request_info(ctypes.Structure):
  50. """A wrapper for struct mg_request_info."""
  51. _fields_ = [
  52. ('request_method', ctypes.c_char_p),
  53. ('uri', ctypes.c_char_p),
  54. ('http_version', ctypes.c_char_p),
  55. ('query_string', ctypes.c_char_p),
  56. ('remote_user', ctypes.c_char_p),
  57. ('remote_ip', ctypes.c_long),
  58. ('remote_port', ctypes.c_int),
  59. ('is_ssl', ctypes.c_int),
  60. ('num_headers', ctypes.c_int),
  61. ('http_headers', mg_header * 64),
  62. ]
  63. mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p)
  64. class Connection(object):
  65. """A wrapper class for all functions that take
  66. struct mg_connection * as the first argument."""
  67. def __init__(self, mongoose, connection):
  68. self.m = mongoose
  69. self.conn = ctypes.c_void_p(connection)
  70. self.info = self.m.dll.mg_get_request_info(self.conn).contents
  71. self.user_data = self.m.dll.mg_get_user_data(self.conn)
  72. self.log_message = self.m.dll.mg_get_log_message(self.conn)
  73. self.reply_status_code = self.m.dll.mg_get_reply_status_code(self.conn)
  74. self.ssl_context = self.m.dll.mg_get_ssl_context(self.conn)
  75. def get_header(self, name):
  76. val = self.m.dll.mg_get_header(self.conn, name)
  77. return ctypes.c_char_p(val).value
  78. def get_var(self, data, name):
  79. size = data and len(data) or 0
  80. buf = ctypes.create_string_buffer(size)
  81. n = self.m.dll.mg_get_var(data, size, name, buf, size)
  82. return n >= 0 and buf or None
  83. def printf(self, fmt, *args):
  84. val = self.m.dll.mg_printf(self.conn, fmt, *args)
  85. return ctypes.c_int(val).value
  86. def write(self, data):
  87. val = self.m.dll.mg_write(self.conn, data, len(data))
  88. return ctypes.c_int(val).value
  89. def read(self, size):
  90. buf = ctypes.create_string_buffer(size)
  91. n = self.m.dll.mg_read(self.conn, buf, size)
  92. return n <= 0 and None or buf[:n]
  93. def send_file(self, path):
  94. self.m.dll.mg_send_file(self.conn, path)
  95. class Mongoose(object):
  96. """A wrapper class for Mongoose shared library."""
  97. def __init__(self, callback, **kwargs):
  98. if os.name == 'nt':
  99. self.dll = ctypes.WinDLL('mongoose.dll')
  100. else:
  101. self.dll = ctypes.CDLL('libmongoose.so')
  102. self.dll.mg_start.restype = ctypes.c_void_p
  103. self.dll.mg_modify_passwords_file.restype = ctypes.c_int
  104. self.dll.mg_read.restype = ctypes.c_int
  105. self.dll.mg_write.restype = ctypes.c_int
  106. self.dll.mg_printf.restype = ctypes.c_int
  107. self.dll.mg_get_header.restype = ctypes.c_char_p
  108. self.dll.mg_get_var.restype = ctypes.c_int
  109. self.dll.mg_get_cookie.restype = ctypes.c_int
  110. self.dll.mg_get_option.restype = ctypes.c_char_p
  111. self.dll.mg_get_request_info.restype = ctypes.POINTER(mg_request_info)
  112. self.dll.mg_get_user_data.restype = ctypes.c_void_p
  113. self.dll.mg_get_reply_status_code.restype = ctypes.c_int
  114. self.dll.mg_get_log_message.restype = ctypes.c_char_p
  115. self.dll.mg_get_ssl_context.restype = ctypes.c_void_p
  116. if callback:
  117. # Create a closure that will be called by the shared library.
  118. def func(event, connection):
  119. # Wrap connection pointer into the connection
  120. # object and call Python callback
  121. conn = Connection(self, connection)
  122. return callback(event, conn) and 1 or 0
  123. # Convert the closure into C callable object
  124. self.callback = mg_callback_t(func)
  125. self.callback.restype = ctypes.c_char_p
  126. else:
  127. self.callback = ctypes.c_void_p(0)
  128. args = [y for x in kwargs.items() for y in x] + [None]
  129. options = (ctypes.c_char_p * len(args))(*args)
  130. ret = self.dll.mg_start(self.callback, 0, options)
  131. self.ctx = ctypes.c_void_p(ret)
  132. def __del__(self):
  133. """Destructor, stop Mongoose instance."""
  134. self.dll.mg_stop(self.ctx)
  135. def get_option(self, name):
  136. return self.dll.mg_get_option(self.ctx, name)