Mercurial > public > mercurial-scm > hg
comparison mercurial/hgweb.py @ 158:be7103467d2e
Add 'hg serve' command for stand-alone server
This adds a simple stand-alone web server mode to hg that exports the
current repo for merging and browsing. The default port is 8000.
author | mpm@selenic.com |
---|---|
date | Wed, 25 May 2005 16:27:10 -0800 |
parents | 2653740d8118 |
children | 7fc8385df514 |
comparison
equal
deleted
inserted
replaced
157:2653740d8118 | 158:be7103467d2e |
---|---|
576 sys.stdout.write(z.flush()) | 576 sys.stdout.write(z.flush()) |
577 | 577 |
578 else: | 578 else: |
579 write(self.t("error")) | 579 write(self.t("error")) |
580 | 580 |
581 if __name__ == "__main__": | 581 def server(path, name, templates, address, port): |
582 hgweb().run() | 582 |
583 import BaseHTTPServer | |
584 import sys, os | |
585 | |
586 class hgwebhandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
587 def do_POST(self): | |
588 self.do_hgweb() | |
589 | |
590 def do_GET(self): | |
591 self.do_hgweb() | |
592 | |
593 def do_hgweb(self): | |
594 query = "" | |
595 p = self.path.find("?") | |
596 if p: | |
597 query = self.path[p + 1:] | |
598 query = query.replace('+', ' ') | |
599 | |
600 env = {} | |
601 env['GATEWAY_INTERFACE'] = 'CGI/1.1' | |
602 env['REQUEST_METHOD'] = self.command | |
603 if query: | |
604 env['QUERY_STRING'] = query | |
605 host = self.address_string() | |
606 if host != self.client_address[0]: | |
607 env['REMOTE_HOST'] = host | |
608 env['REMOTE_ADDR'] = self.client_address[0] | |
609 | |
610 if self.headers.typeheader is None: | |
611 env['CONTENT_TYPE'] = self.headers.type | |
612 else: | |
613 env['CONTENT_TYPE'] = self.headers.typeheader | |
614 length = self.headers.getheader('content-length') | |
615 if length: | |
616 env['CONTENT_LENGTH'] = length | |
617 accept = [] | |
618 for line in self.headers.getallmatchingheaders('accept'): | |
619 if line[:1] in "\t\n\r ": | |
620 accept.append(line.strip()) | |
621 else: | |
622 accept = accept + line[7:].split(',') | |
623 env['HTTP_ACCEPT'] = ','.join(accept) | |
624 | |
625 os.environ.update(env) | |
626 | |
627 save = sys.argv, sys.stdin, sys.stdout, sys.stderr | |
628 try: | |
629 sys.stdin = self.rfile | |
630 sys.stdout = self.wfile | |
631 sys.argv = ["hgweb.py"] | |
632 if '=' not in query: | |
633 sys.argv.append(query) | |
634 self.send_response(200, "Script output follows") | |
635 hg.run() | |
636 finally: | |
637 sys.argv, sys.stdin, sys.stdout, sys.stderr = save | |
638 | |
639 hg = hgweb(path, name, templates) | |
640 httpd = BaseHTTPServer.HTTPServer((address, port), hgwebhandler) | |
641 httpd.serve_forever() |