Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CGIT Improvements #155

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
orbit: cgit: handle cgi response more correctly
Without the embedded option in the cgitrc cgit generates a full html page
with its own html and body tags that we were embedding within our existing
body creating ill-formed html documents.

We can also use noheader to suppress the header bar and avoid the need to
hide the logo with custom css rules.

By parsing the http headers returned from cgit we can also surface
information like content type to the client and suppress wrapping the
output in our html if it is supposed to be binary data which allows us to
bring back the plain view button.
charliemirabile committed Sep 4, 2024
commit 48887b9759e98e9684be133a417c25619f70baa9
20 changes: 5 additions & 15 deletions orbit/cgitrc
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
# Specify the css url
css=/style.css

# Allow http transport git clone
enable-http-clone=0

# Enable caching of up to 1000 output entries
cache-size=0

# Use a custom logo
logo=/images/kdlp_logo.png

# Set the title and heading of the repository index page
root-title=Kernel Development Learning Pipeline Git Repositories

# Set a subheading for the repository index page
root-desc=Explore the class git repositories here!

# Allow cgit to use git config to set repo-specific settings
enable-git-config=1


# Create output suitable for embedding within other pages
embedded=1
noheader=1

##
## List of common mimetypes
##
41 changes: 30 additions & 11 deletions orbit/radius.py
Original file line number Diff line number Diff line change
@@ -450,24 +450,43 @@ def handle_cgit(rocket):
cgit_env['PATH_INFO'] = rocket.path_info.removeprefix('/cgit')
cgit_env['QUERY_STRING'] = rocket.env.get('QUERY_STRING', '')

path_array = cgit_env['PATH_INFO'].split('/')
if len(path_array) > 2 and path_array[2] == 'plain':
return rocket.raw_respond(HTTPStatus.NOT_FOUND)
def cgit_internal_server_error(msg):
print(f'cgit: Error {msg} at path_info "{cgit_env["PATH_INFO"]}"'
f' and query string "{cgit_env["QUERY_STRING"]}"',
file=sys.stderr)
return rocket.raw_respond(HTTPStatus.INTERNAL_SERVER_ERROR)

proc = subprocess.Popen(['/usr/share/webapps/cgit/cgit'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=cgit_env)
so, se = proc.communicate()
try:
outstring = so.decode()
begin = outstring.index('\n\n')
return rocket.respond(outstring[begin+2:])
except (UnicodeDecodeError, ValueError) as ex:
print(f'cgit: Error {type(ex)} at path_info "{cgit_env["PATH_INFO"]}"'
f' and query string "{cgit_env["QUERY_STRING"]}"',
file=sys.stderr)
return rocket.raw_respond(HTTPStatus.INTERNAL_SERVER_ERROR)
raw_headers, raw_body = so.split(b'\n\n', maxsplit=1)
headers_text = raw_headers.decode()
headers = [tuple(line.split(': ', maxsplit=1))
for line in headers_text.split('\n')]
raw_return = False
status = HTTPStatus.OK
if headers[0][0] == 'Status':
status_str = headers[0][1]
status = HTTPStatus(int(status_str.split(' ')[0]))
if status == HTTPStatus.OK:
return cgit_internal_server_error('Unexpected 200 status')
raw_return = True
raw_body = b''
del headers[0]
if headers[0][0] != 'Content-Type':
return cgit_internal_server_error('missing Content-Type')
if headers[0][1] != 'text/html; charset=UTF-8':
raw_return = True
rocket.headers += headers
if raw_return:
return rocket.raw_respond(status, raw_body)
outstring = raw_body.decode()
return rocket.respond(outstring)
except (UnicodeDecodeError, ValueError, IndexError) as ex:
return cgit_internal_server_error(type(ex))


def handle_error(rocket):