Exemplo n.º 1
0
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
}
Exemplo n.º 2
0
// PyObject* PyBool_FromLong(long v)
// Return value: New reference.
// Return a new reference to Py_True or Py_False depending on the truth value of v.
//
// New in version 2.3.
func PyBool_FromLong(v int) *PyObject {
	return togo(C.PyBool_FromLong(C.long(v)))
}
Exemplo n.º 3
0
// encode translates a Go value (or a wrapped Python object) to a Python
// object.
func encode(x interface{}) (pyValue *C.PyObject, err error) {
	if x == nil {
		pyValue = C.None_INCREF()
		return
	}

	switch value := x.(type) {
	case bool:
		var i C.long
		if value {
			i = 1
		}
		pyValue = C.PyBool_FromLong(i)

	case byte: // alias uint8
		c := C.char(value)
		pyValue = C.PyString_FromStringAndSize(&c, 1)

	case complex64:
		pyValue = C.PyComplex_FromDoubles(C.double(real(value)), C.double(imag(value)))

	case complex128:
		pyValue = C.PyComplex_FromDoubles(C.double(real(value)), C.double(imag(value)))

	case float32:
		pyValue = C.PyFloat_FromDouble(C.double(value))

	case float64:
		pyValue = C.PyFloat_FromDouble(C.double(value))

	case int: // alias rune
		pyValue = C.PyInt_FromLong(C.long(value))

	case int8:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int16:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int32:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int64:
		pyValue = C.Long_FromInt64(C.int64_t(value))

	case string:
		pyValue = C.String_FromGoStringPtr(unsafe.Pointer(&value))

	case uint:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uint16:
		pyValue = C.PyInt_FromLong(C.long(value))

	case uint32:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uint64:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uintptr:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case []interface{}:
		return encodeTuple(value)

	case map[interface{}]interface{}:
		return encodeDict(value)

	case *object:
		pyValue = value.pyObject
		C.INCREF(pyValue)

	default:
		err = fmt.Errorf("unable to translate %t to Python", x)
		return
	}

	if pyValue == nil {
		err = getError()
	}
	return
}