Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simple HN Proxy

  • Loading branch information...
commit 91b186f3d9dabb7eec082716007ea9f9eeb25a8e 0 parents
@matthusby authored
Showing with 88 additions and 0 deletions.
  1. +8 −0 app.yaml
  2. +11 −0 index.yaml
  3. +26 −0 main.py
  4. +43 −0 proxyHandler.py
8 app.yaml
@@ -0,0 +1,8 @@
+application: quickhn
+version: 1
+runtime: python
+api_version: 1
+
+handlers:
+- url: .*
+ script: main.py
11 index.yaml
@@ -0,0 +1,11 @@
+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run. If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED"). If you want to manage some indexes
+# manually, move them above the marker line. The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
26 main.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+#
+# Copyright 2007 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from google.appengine.ext import webapp
+from google.appengine.ext.webapp import util
+from proxyHandler import AssestHandler
+
+def main():
+ application = webapp.WSGIApplication([('/.*', AssestHandler)], debug=True)
+ util.run_wsgi_app(application)
+
+if __name__ == '__main__':
+ main()
43 proxyHandler.py
@@ -0,0 +1,43 @@
+from google.appengine.api import urlfetch
+from google.appengine.ext import webapp
+
+class AssestHandler(webapp.RequestHandler):
+ def get(self):
+ url = "http://news.ycombinator.com" + self.request.path
+ if self.request.query_string is not None:
+ url = url + '?' + self.request.query_string
+
+ if 'user' in self.request.cookies:
+ fetch_headers = {'Cookie':'user=' + self.request.cookies['user']}
+ result = urlfetch.fetch(url, headers=fetch_headers, follow_redirects=False)
+ else:
+ result = urlfetch.fetch(url, follow_redirects=False)
+
+ if 'set-cookie' in result.headers:
+ self.response.headers.add_header('set-cookie', result.headers.get('set-cookie'))
+
+ if 'location' in result.headers:
+ self.redirect('/' + result.headers.get('location'))
+ else:
+ self.response.headers.add_header('content-type', result.headers['content-type'])
+ self.response.out.write(result.content)
+
+ def post(self):
+ url = "http://news.ycombinator.com" + self.request.path
+ post_data = ''
+ for arg in self.request.arguments():
+ post_data += arg + '=' + self.request.get(arg) + '&'
+ post_data = post_data[:-1]
+ if 'user' in self.request.cookies:
+ fetch_headers = {'Cookie':'user=' + self.request.cookies['user']}
+ result = urlfetch.fetch(url, method=urlfetch.POST, headers=fetch_headers, payload=post_data, follow_redirects=False)
+ else:
+ result = urlfetch.fetch(url, method=urlfetch.POST, payload=post_data, follow_redirects=False)
+
+ if 'set-cookie' in result.headers:
+ self.response.headers.add_header('set-cookie', result.headers.get('set-cookie'))
+
+ if 'location' in result.headers:
+ self.redirect('/' + result.headers.get('location'))
+ else:
+ self.response.out.write(result.content)
Please sign in to comment.
Something went wrong with that request. Please try again.