[Featureserver] D in CRUD not working

Yves Moisan yves.moisan at boreal-is.com
Tue Dec 23 11:39:35 EST 2008


> Making it do so is hard, because urllib doesn't make anything other than
> get and post easy, so you have to drop down to the level of httplib.

I found out about httplib2 and it works nicely.  I haven't looked at all
its potential so I don't know if it could replace urllib2 altogether,
but at least it allows me to update the OpenLayers proxy so that it
accepts DELETE requests.  

I guess I could explicitly take care of the response if a problem
happens ... Downside is httplib2 is not part of the standard library,
but it's easy to install.  Anyhow, here's the proxy code revisited, if
you want it :

#!C:\Python24\python -u


"""This is a blind proxy that we use to get around browser
restrictions that prevent the Javascript from loading pages not on the
same server as the Javascript.  This has several problems: it's less
efficient, it might break some sites, and it's a security risk because
people can use this proxy to browse the web and possibly do bad stuff
with it.  It only loads pages via http and https, but it can load any
content type. It supports GET, POST *** and DELETE *** requests."""

import urllib2, httplib2
import cgi
import sys, os

# Designed to prevent Open Proxy type stuff.


allowedHosts = ['www.openlayers.org', 'openlayers.org',
'demo.mapfish.org',
                'xxx.xxx.x.156:8586',
                'xxx.xxx.x.124:8084',
                'xxx.xxx.x.124:8088',
                'xxx.xxx.x.124:8080',
                'fgmapp2.fmi.com:8081','www.openlayers.org',
'openlayers.org', 
                'labs.metacarta.com', 'world.freemap.in', 
                'prototype.openmnnd.org', 'geo.openplans.org',
                'sigma.openplans.org'
                'www.openstreetmap.org']

method = os.environ["REQUEST_METHOD"]
if method == "POST" or method == "DELETE":
    qs = os.environ["QUERY_STRING"]
    d = cgi.parse_qs(qs)
    if d.has_key("url"):
        url = d["url"][0]
    else:
        url = "http://www.openlayers.org"
else:
    fs = cgi.FieldStorage()
    url = fs.getvalue('url', "http://www.openlayers.org")

try:
    host = url.split("/")[2]
    if allowedHosts and not host in allowedHosts:
        print "Status: 502 Bad Gateway"
        print "Content-Type: text/plain"
        print
        print "This proxy does not allow you to access that location."
        print 
        print os.environ
  
    elif url.startswith("http://") or url.startswith("https://"):
    
        if method == "POST":
            length = int(os.environ["CONTENT_LENGTH"])
            headers = {"Content-Type": os.environ["CONTENT_TYPE"]}
            body = sys.stdin.read(length)
            r = urllib2.Request(url, body, headers)
            y = urllib2.urlopen(r)
		
        elif method == "DELETE":
            h = httplib2.Http(".cache")
            (resp, content) = h.request(url,"DELETE")
            # resp has the response like : 
            # >>> resp
            # >>> {'date': 'Tue, 23 Dec 2008 16:08:21 GMT', 'status':
'200', 'content-length': '58', 'content-type': 'text/plain', 'server':
'WSGIServer/0.1 Python/2.4.4'}
            # >>> content
            # >>> '{"crs": null, "type": "FeatureCollection",
"features": []}'
            # 
            # So 'i.has_key("Content-Type"') is equivalent to
'resp.has_key("content-type")'
            #
            # and 'y.read()' is equivalent to 'content'
        else:
            y = urllib2.urlopen(url)
        
        # print content type header
        i = y.info()
        if i.has_key("Content-Type"):
            print "Content-Type: %s" % (i["Content-Type"])
        else:
            print "Content-Type: text/plain"
        print
        
        print y.read()
        
        y.close()
    else:
        print "Content-Type: text/plain"
        print
        print "Illegal request."

except Exception, E:
    print "Status: 500 Unexpected Error"
    print "Content-Type: text/plain"
    print 
    print "Some unexpected error occurred. Error text was:", E





More information about the Featureserver mailing list