func GenerateEnviron(r *http.Request) *C.PyObject { _environ := C.PyDict_New() for k, _items := range r.Header { _values_tuple := C.PyTuple_New(C.Py_ssize_t(len(_items))) for i, _item := range _items { C.PyTuple_SetItem( _values_tuple, C.Py_ssize_t(i), PyString_FromString(_item), ) } C.PyDict_SetItem( _environ, PyString_FromString(k), _values_tuple, ) } //_environ = upgrade_to_wsgi(r, _environ) return _environ }
func interfaceToPyObj(o interface{}) *C.PyObject { switch o.(type) { case int: return C.PyInt_FromLong(C.long(o.(int))) case int64: return C.PyInt_FromLong(C.long(o.(int64))) case string: strvalue := C.CString(o.(string)) defer C.free(unsafe.Pointer(strvalue)) return C.PyString_FromStringAndSize(strvalue, C.Py_ssize_t(len(o.(string)))) case map[interface{}]interface{}: dict := C.PyDict_New() for key, value := range o.(map[interface{}]interface{}) { dictAddItem(dict, key, value) } return dict case map[string]string: dict := C.PyDict_New() for key, value := range o.(map[string]string) { dictAddItem(dict, key, value) } return dict case map[string]interface{}: dict := C.PyDict_New() for key, value := range o.(map[string]interface{}) { dictAddItem(dict, key, value) } return dict case []interface{}: list := C.PyTuple_New(C.Py_ssize_t(len(o.([]interface{})))) for i := range o.([]interface{}) { C.PyTuple_SetItem(list, C.Py_ssize_t(i), interfaceToPyObj(o.([]interface{})[i])) } return list default: return C.PyNone() } }
// int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o) // Insert a reference to object o at position pos of the tuple pointed to by p. Return 0 on success. // // Note This function “steals” a reference to o. // Changed in version 2.5: This function used an int type for pos. This might require changes in your code for properly supporting 64-bit systems. func PyTuple_SetItem(self *PyObject, pos int, o *PyObject) error { return int2err(C.PyTuple_SetItem(topy(self), C.Py_ssize_t(pos), topy(o))) }
func (t *Tuple) SetItem(pos int64, obj Object) error { ret := C.PyTuple_SetItem(c(t), C.Py_ssize_t(pos), c(obj)) return int2Err(ret) }
func upgrade_header_to_wsgi(r *http.Request) *C.PyObject { _environ := C.PyDict_New() for k, _items := range r.Header { _values_tuple := C.PyTuple_New(C.Py_ssize_t(len(_items))) for i, _item := range _items { C.PyTuple_SetItem( _values_tuple, C.Py_ssize_t(i), PyString_FromString(_item), ) } // convert header name _k := strings.ToUpper(strings.Replace(k, "-", "_", -1)) C.PyDict_SetItem( _environ, PyString_FromString("HTTP_"+_k), _values_tuple, ) } C.PyDict_SetItem( _environ, PyString_FromString("X_FROM"), PyString_FromString("gowsgi"), ) C.PyDict_SetItem( _environ, PyString_FromString("REQUEST_METHOD"), PyString_FromString(r.Method), ) C.PyDict_SetItem( _environ, PyString_FromString("SCRIPT_NAME"), PyString_FromString(r.URL.Path), ) C.PyDict_SetItem( _environ, PyString_FromString("PATH_INFO"), PyString_FromString(r.URL.Path), ) C.PyDict_SetItem( _environ, PyString_FromString("QUERY_STRING"), PyString_FromString(r.URL.RawQuery), ) C.PyDict_SetItem( _environ, PyString_FromString("CONTENT_TYPE"), PyString_FromString(""), ) C.PyDict_SetItem( _environ, PyString_FromString("CONTENT_LENGTH"), PyString_FromString("0"), ) _host, _port, _ := net.SplitHostPort(r.Host) C.PyDict_SetItem( _environ, PyString_FromString("SERVER_NAME"), PyString_FromString(_host), ) C.PyDict_SetItem( _environ, PyString_FromString("SERVER_PORT"), PyString_FromString(_port), ) C.PyDict_SetItem( _environ, PyString_FromString("SERVER_PROTOCOL"), PyString_FromString(r.Proto), ) C.PyDict_SetItem( _environ, PyString_FromString("wsgi.url_scheme"), PyString_FromString(strings.ToLower(strings.Split(r.Proto, "/")[0])), ) C.PyDict_SetItem( _environ, PyString_FromString("wsgi.multithread"), C.PyBool_FromLong(1), ) C.PyDict_SetItem( _environ, PyString_FromString("wsgi.multiprocess"), C.PyBool_FromLong(1), ) C.PyDict_SetItem( _environ, PyString_FromString("wsgi.run_once"), C.PyBool_FromLong(0), ) return _environ }
// Insert a reference to object o at position pos of the tuple pointed to by p. // Return 0 on success. // Note This function “steals” a reference to o. func (self *Tuple) SetItem(pos int, obj *PyObject) int { ret := C.PyTuple_SetItem(self.ptr, C.Py_ssize_t(pos), obj.ptr) return (int)(ret) // return int2Err(ret) }
// Insert a reference to object o at position pos of the tuple pointed to by p. Return 0 on success. // Note This function “steals” a reference to o. func (t *Tuple) SetItem(pos int, obj *Base) error { ret := C.PyTuple_SetItem(t.c(), C.Py_ssize_t(pos), obj.c()) return int2Err(ret) }