| 1 |
# An example WSGI script for IIS/isapi-wsgi to export multiple hgweb repos |
| 2 |
# Copyright 2010-2016 Sune Foldager <[email protected]> |
| 3 |
# |
| 4 |
# This software may be used and distributed according to the terms of the |
| 5 |
# GNU General Public License version 2 or any later version. |
| 6 |
# |
| 7 |
# Requirements: |
| 8 |
# - Python 2.7, preferably 64 bit |
| 9 |
# - Mercurial installed from source (python setup.py install) or download the |
| 10 |
# python module installer from https://www.mercurial-scm.org/wiki/Download |
| 11 |
# - IIS 7 or newer |
| 12 |
# |
| 13 |
# |
| 14 |
# Installation and use: |
| 15 |
# |
| 16 |
# - Download or clone the isapi-wsgi source and run python setup.py install. |
| 17 |
# https://github.com/hexdump42/isapi-wsgi |
| 18 |
# |
| 19 |
# - Create a directory to hold the shim dll, config files etc. This can reside |
| 20 |
# inside the standard IIS directory, C:\inetpub, or anywhere else. Copy this |
| 21 |
# script there. |
| 22 |
# |
| 23 |
# - Run this script (i.e. python hgwebdir_wsgi.py) to get a shim dll. The |
| 24 |
# shim is identical for all scripts, so you can just copy and rename one |
| 25 |
# from an earlier run, if you wish. The shim needs to reside in the same |
| 26 |
# directory as this script. |
| 27 |
# |
| 28 |
# - Start IIS manager and create a new app pool: |
| 29 |
# .NET CLR Version: No Managed Code |
| 30 |
# Advanced Settings: Enable 32 Bit Applications, if using 32 bit Python. |
| 31 |
# You can adjust the identity and maximum worker processes if you wish. This |
| 32 |
# setup works fine with multiple worker processes. |
| 33 |
# |
| 34 |
# - Create an IIS application where your hgwebdir is to be served from. |
| 35 |
# Assign it the app pool you just created and point its physical path to the |
| 36 |
# directory you created. |
| 37 |
# |
| 38 |
# - In the application, remove all handler mappings and setup a wildcard script |
| 39 |
# handler mapping of type IsapiModule with the shim dll as its executable. |
| 40 |
# This file MUST reside in the same directory as the shim. The easiest way |
| 41 |
# to do all this is to close IIS manager, place a web.config file in your |
| 42 |
# directory and start IIS manager again. The file should contain: |
| 43 |
# |
| 44 |
# <?xml version="1.0" encoding="UTF-8"?> |
| 45 |
# <configuration> |
| 46 |
# <system.webServer> |
| 47 |
# <handlers accessPolicy="Read, Script"> |
| 48 |
# <clear /> |
| 49 |
# <add name="hgwebdir" path="*" verb="*" modules="IsapiModule" |
| 50 |
# scriptProcessor="C:\your\directory\_hgwebdir_wsgi.dll" |
| 51 |
# resourceType="Unspecified" requireAccess="None" |
| 52 |
# preCondition="bitness64" /> |
| 53 |
# </handlers> |
| 54 |
# </system.webServer> |
| 55 |
# </configuration> |
| 56 |
# |
| 57 |
# Where "bitness64" should be replaced with "bitness32" for 32 bit Python. |
| 58 |
# |
| 59 |
# - Edit ISAPI And CGI Restrictions on the web server (global setting). Add a |
| 60 |
# restriction pointing to your shim dll and allow it to run. |
| 61 |
# |
| 62 |
# - Create a configuration file in your directory and adjust the configuration |
| 63 |
# variables below to match your needs. Example configuration: |
| 64 |
# |
| 65 |
# [web] |
| 66 |
# style = gitweb |
| 67 |
# push_ssl = false |
| 68 |
# allow_push = * |
| 69 |
# encoding = utf8 |
| 70 |
# |
| 71 |
# [server] |
| 72 |
# validate = true |
| 73 |
# |
| 74 |
# [paths] |
| 75 |
# repo1 = c:\your\directory\repo1 |
| 76 |
# repo2 = c:\your\directory\repo2 |
| 77 |
# |
| 78 |
# - Restart the web server and see if things are running. |
| 79 |
# |
| 80 |
|
| 81 |
|
| 82 |
# Configuration file location |
| 83 |
hgweb_config = r'c:\your\directory\wsgi.config' |
| 84 |
|
| 85 |
# Global settings for IIS path translation |
| 86 |
path_strip = 0 # Strip this many path elements off (when using url rewrite) |
| 87 |
path_prefix = 1 # This many path elements are prefixes (depends on the |
| 88 |
# virtual path of the IIS application). |
| 89 |
|
| 90 |
import sys |
| 91 |
|
| 92 |
# Adjust python path if this is not a system-wide install |
| 93 |
# sys.path.insert(0, r'C:\your\custom\hg\build\lib.win32-2.7') |
| 94 |
|
| 95 |
# Enable tracing. Run 'python -m win32traceutil' to debug |
| 96 |
if getattr(sys, 'isapidllhandle', None) is not None: |
| 97 |
import win32traceutil |
| 98 |
|
| 99 |
win32traceutil.SetupForPrint # silence unused import warning |
| 100 |
|
| 101 |
import isapi_wsgi |
| 102 |
from mercurial.hgweb.hgwebdir_mod import hgwebdir |
| 103 |
|
| 104 |
|
| 105 |
# Example tweak: Replace isapi_wsgi's handler to provide better error message |
| 106 |
# Other stuff could also be done here, like logging errors etc. |
| 107 |
class WsgiHandler(isapi_wsgi.IsapiWsgiHandler): |
| 108 |
error_status = '500 Internal Server Error' # less silly error message |
| 109 |
|
| 110 |
|
| 111 |
isapi_wsgi.IsapiWsgiHandler = WsgiHandler |
| 112 |
|
| 113 |
# Only create the hgwebdir instance once |
| 114 |
application = hgwebdir(hgweb_config) |
| 115 |
|
| 116 |
|
| 117 |
def handler(environ, start_response): |
| 118 |
# Translate IIS's weird URLs |
| 119 |
url = environ['SCRIPT_NAME'] + environ['PATH_INFO'] |
| 120 |
paths = url[1:].split('/')[path_strip:] |
| 121 |
script_name = '/' + '/'.join(paths[:path_prefix]) |
| 122 |
path_info = '/'.join(paths[path_prefix:]) |
| 123 |
if path_info: |
| 124 |
path_info = '/' + path_info |
| 125 |
environ['SCRIPT_NAME'] = script_name |
| 126 |
environ['PATH_INFO'] = path_info |
| 127 |
|
| 128 |
return application(environ, start_response) |
| 129 |
|
| 130 |
|
| 131 |
def __ExtensionFactory__(): |
| 132 |
return isapi_wsgi.ISAPISimpleHandler(handler) |
| 133 |
|
| 134 |
|
| 135 |
if __name__ == '__main__': |
| 136 |
from isapi.install import ISAPIParameters, HandleCommandLine |
| 137 |
|
| 138 |
params = ISAPIParameters() |
| 139 |
HandleCommandLine(params) |