mongoose.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. class mg_header(ctypes.Structure):
  35. """A wrapper for struct mg_header."""
  36. _fields_ = [
  37. ('name', ctypes.c_char_p),
  38. ('value', ctypes.c_char_p),
  39. ]
  40. class mg_request_info(ctypes.Structure):
  41. """A wrapper for struct mg_request_info."""
  42. _fields_ = [
  43. ('request_method', ctypes.c_char_p),
  44. ('uri', ctypes.c_char_p),
  45. ('http_version', ctypes.c_char_p),
  46. ('query_string', ctypes.c_char_p),
  47. ('remote_user', ctypes.c_char_p),
  48. ('log_message', ctypes.c_char_p),
  49. ('remote_ip', ctypes.c_long),
  50. ('remote_port', ctypes.c_int),
  51. ('status_code', ctypes.c_int),
  52. ('is_ssl', ctypes.c_int),
  53. ('num_headers', ctypes.c_int),
  54. ('http_headers', mg_header * 64),
  55. ]
  56. mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p,
  57. ctypes.c_void_p,
  58. ctypes.POINTER(mg_request_info))
  59. class Connection(object):
  60. """A wrapper class for all functions that take
  61. struct mg_connection * as the first argument."""
  62. def __init__(self, mongoose, connection):
  63. self.m = mongoose
  64. self.conn = ctypes.c_void_p(connection)
  65. def get_header(self, name):
  66. val = self.m.dll.mg_get_header(self.conn, name)
  67. return ctypes.c_char_p(val).value
  68. def get_var(self, data, name):
  69. size = len(data)
  70. buf = ctypes.create_string_buffer(size)
  71. n = self.m.dll.mg_get_var(data, size, name, buf, size)
  72. return n == MG_SUCCESS and buf or None
  73. def printf(self, fmt, *args):
  74. val = self.m.dll.mg_printf(self.conn, fmt, *args)
  75. return ctypes.c_int(val).value
  76. def write(self, data):
  77. val = self.m.dll.mg_write(self.conn, data, len(data))
  78. return ctypes.c_int(val).value
  79. def read(self, size):
  80. buf = ctypes.create_string_buffer(size)
  81. n = self.m.dll.mg_read(self.conn, buf, size)
  82. print size, buf, n
  83. return n <= 0 and None or buf[:n]
  84. class Mongoose(object):
  85. """A wrapper class for Mongoose shared library."""
  86. def __init__(self, callback, **kwargs):
  87. dll_extension = os.name == 'nt' and 'dll' or 'so'
  88. self.dll = ctypes.CDLL('_mongoose.%s' % dll_extension)
  89. self.dll.mg_start.restype = ctypes.c_void_p
  90. self.dll.mg_modify_passwords_file.restype = ctypes.c_int
  91. self.dll.mg_read.restype = ctypes.c_int
  92. self.dll.mg_write.restype = ctypes.c_int
  93. self.dll.mg_printf.restype = ctypes.c_int
  94. self.dll.mg_get_header.restype = ctypes.c_char_p
  95. self.dll.mg_get_var.restype = ctypes.c_int
  96. self.dll.mg_get_cookie.restype = ctypes.c_int
  97. self.dll.mg_get_option.restype = ctypes.c_char_p
  98. if callback:
  99. # Create a closure that will be called by the shared library.
  100. def func(connection, request_info):
  101. # Wrap connection pointer into the connection
  102. # object and call Python callback
  103. conn = Connection(self, connection)
  104. if python_func(conn, request_info.contents):
  105. return 'non-null-pointer'
  106. else:
  107. return ctypes.c_void_p(0)
  108. # Convert the closure into C callable object
  109. self.callback = mg_callback_t(func)
  110. self.callback.restype = ctypes.c_void_p
  111. else:
  112. self.callback = ctypes.c_void_p(0)
  113. args = [y for x in kwargs.items() for y in x] + [None]
  114. options = (ctypes.c_char_p * len(args))(*args)
  115. # self.ctx = self.dll.mg_start(self.callback, options)
  116. self.ctx = self.dll.mg_start(ctypes.c_void_p(0), options)
  117. def __del__(self):
  118. """Destructor, stop Mongoose instance."""
  119. self.dll.mg_stop(ctypes.c_void_p(self.ctx))
  120. def get_option(self, name):
  121. return self.dll.mg_get_option(self.ctx, name)