1. Ruslan Osmanov
  2. pecl-event

Commits

Ruslan Osmanov  committed 1c69239 Merge

Merged in rickysu/pecl-event/master (pull request #2)

EventHttpRequest add some method

  • Participants
  • Parent commits 24bb187, ad484d9
  • Branches master

Comments (0)

Files changed (6)

File classes/buffer_event.c

View file
  • Ignore whitespace
 				"Failed to allocate bufferevent for socket");
 		return;
 	}
-
+        bev->_internal = 0;
 	bev->bevent = bevent;
 
 	bev->self = zself;
 	PHP_EVENT_FETCH_BEVENT(bev, zbevent);
 
 	if (bev->bevent) {
-		bufferevent_free(bev->bevent);
+                if(!bev->_internal){
+		    bufferevent_free(bev->bevent);
+                }
 		bev->bevent = 0;
 
 		/* Do it once */
 		return;
 	}
 #endif
-	
+
 	if (family & ~(AF_INET | AF_INET6 | AF_UNSPEC)) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING,
 				"Invalid address family specified");
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OZOl|l",
 				&zbase, php_event_base_ce,
-				&ppzfd, 
+				&ppzfd,
 				&zctx, php_event_ssl_context_ce,
 				&state, &options) == FAILURE) {
 		return;
 
 	e = bufferevent_get_openssl_error(bev->bevent);
 	if (e) {
-		RETURN_STRING(ERR_error_string(e, buf), 1);	
+		RETURN_STRING(ERR_error_string(e, buf), 1);
 	}
 
 	RETVAL_FALSE;

File classes/http_request.c

View file
  • Ignore whitespace
 	zend_fcall_info_cache  fcc      = empty_fcall_info_cache;
 	zval                  *zarg     = NULL;
 	struct evhttp_request *req;
-	
+
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|z",
 				&fci, &fcc, &zarg) == FAILURE) {
 }
 /* }}} */
 
+
+/* {{{ proto EventBufferEvent EventHttpRequest::getEventBufferEvent(void);
+ * Returns EventBufferEvent object. */
+PHP_METHOD(EventHttpRequest, getEventBufferEvent)
+{
+	php_event_http_req_t *http_req;
+        struct evhttp_connection *conn;
+        php_event_bevent_t *bev;
+	if (zend_parse_parameters_none() == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_HTTP_REQ(http_req, getThis());
+
+	_check_http_req_ptr(http_req);
+
+	PHP_EVENT_INIT_CLASS_OBJECT(return_value, php_event_bevent_ce);
+	PHP_EVENT_FETCH_BEVENT(bev, return_value);
+	conn = evhttp_request_get_connection(http_req->ptr);
+	bev->bevent = evhttp_connection_get_bufferevent(conn);
+	bev->self = return_value;
+	Z_ADDREF_P(return_value);
+	bev->input = bev->output = NULL;
+        bev->_internal = 1;
+}
+/* }}} */
+
+/* {{{ proto EventHttpConnection EventHttpRequest::getEventHttpConnection(void);
+ * Returns EventHttpConnection object. */
+PHP_METHOD(EventHttpRequest, getEventHttpConnection)
+{
+	php_event_http_req_t *http_req;
+        struct evhttp_connection *conn;
+        php_event_http_conn_t    *evcon;
+
+	if (zend_parse_parameters_none() == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_HTTP_REQ(http_req, getThis());
+
+	_check_http_req_ptr(http_req);
+
+	PHP_EVENT_INIT_CLASS_OBJECT(return_value, php_event_http_conn_ce);
+        PHP_EVENT_FETCH_HTTP_CONN(evcon, return_value);
+	conn = evhttp_request_get_connection(http_req->ptr);
+	evcon->conn = conn;
+	evcon->base = NULL;
+        evcon->dns_base = NULL;
+	Z_ADDREF_P(return_value);
+}
+/* }}} */
+
+
+/* {{{ proto void EventHttpRequest::closeConnection(void);
+ */
+PHP_METHOD(EventHttpRequest, closeConnection)
+{
+	php_event_http_req_t *http_req;
+        struct evhttp_connection *conn;
+
+	if (zend_parse_parameters_none() == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_HTTP_REQ(http_req, getThis());
+
+	_check_http_req_ptr(http_req);
+
+	conn = evhttp_request_get_connection(http_req->ptr);
+	evhttp_connection_free(conn);
+//	evhttp_connection_done(conn);
+}
+/* }}} */
+
+
+
 /* {{{ proto void EventHttpRequest::sendError(int error[, string reason = NULL]);
  * Send an HTML error message to the client.
  */
 /* }}} */
 
 /* {{{ proto void EventHttpRequest::sendReplyEnd(void);
- * Complete a chunked reply, freeing the request as appropriate. 
+ * Complete a chunked reply, freeing the request as appropriate.
  */
 PHP_METHOD(EventHttpRequest, sendReplyEnd)
 {
  *
  * Cancels an ongoing HTTP request. The callback associated with this request
  * is not executed and the request object is freed. If the request is currently
- * being processed, e.g. it is ongoing, the corresponding EventHttpConnection 
+ * being processed, e.g. it is ongoing, the corresponding EventHttpConnection
  * object is going to get reset.
  *
  * A request cannot be canceled if its callback has executed already. A request

File src/fe.c

View file
  • Ignore whitespace
 ZEND_END_ARG_INFO();
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_evbuffer_add, 0, 0, 1)
-	ZEND_ARG_INFO(0, data) 
+	ZEND_ARG_INFO(0, data)
 ZEND_END_ARG_INFO();
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_evbuffer_add_buffer, 0, 0, 1)
 	PHP_MALIAS(Event, delTimer,  del, arginfo_event__void, ZEND_ACC_PUBLIC)
 	PHP_MALIAS(Event, addSignal, add, arginfo_event_add,   ZEND_ACC_PUBLIC)
 	PHP_MALIAS(Event, delSignal, del, arginfo_event__void, ZEND_ACC_PUBLIC)
-													  
+
 	PHP_FE_END
 };
 /* }}} */
 #if LIBEVENT_VERSION_NUMBER >= 0x02010200
 	PHP_ME(EventBase, resume,             arginfo_event__void,              ZEND_ACC_PUBLIC)
 #endif
-														   
+
 	PHP_FE_END
 };
 /* }}} */
 	PHP_ME(EventHttpRequest, clearHeaders,     arginfo_event__void,                     ZEND_ACC_PUBLIC)
 	PHP_ME(EventHttpRequest, removeHeader,     arginfo_event_http_req_remove_header,    ZEND_ACC_PUBLIC)
 	PHP_ME(EventHttpRequest, findHeader,       arginfo_event_http_req_remove_header,    ZEND_ACC_PUBLIC)
+	PHP_ME(EventHttpRequest, getEventBufferEvent,   arginfo_event__void,                ZEND_ACC_PUBLIC)
+        PHP_ME(EventHttpRequest, getEventHttpConnection,   arginfo_event__void,                ZEND_ACC_PUBLIC)
+        PHP_ME(EventHttpRequest, closeConnection,   arginfo_event__void,                ZEND_ACC_PUBLIC)
 
 	PHP_FE_END
 };

File src/fe.h

View file
  • Ignore whitespace
 PHP_METHOD(EventHttpRequest, clearHeaders);
 PHP_METHOD(EventHttpRequest, removeHeader);
 PHP_METHOD(EventHttpRequest, findHeader);
+PHP_METHOD(EventHttpRequest, getEventBufferEvent);
+PHP_METHOD(EventHttpRequest, getEventHttpConnection);
+PHP_METHOD(EventHttpRequest, closeConnection);
+
 /* Extra API END }}} */
 #endif
 
 #endif /* PHP_EVENT_FE_H */
 
-/* 
+/*
  * Local variables:
  * tab-width: 4
  * c-basic-offset: 4

File src/pe.c

View file
  • Ignore whitespace
 	PHP_EVENT_PROP_REQUIRE(b->buf);
 
 	MAKE_STD_ZVAL(*retval);
-	ZVAL_LONG(*retval, evbuffer_get_length(b->buf));
+	if(b && b->buf){
+	    ZVAL_LONG(*retval, evbuffer_get_length(b->buf));
+	}
+	else{
+	    ZVAL_LONG(*retval, 0);
+	}
 
 	return SUCCESS;
 }

File src/structs.h

View file
  • Ignore whitespace
 #ifndef PHP_EVENT_STRUCTS_H
 #define PHP_EVENT_STRUCTS_H
 
-/* Thread context. With it we are getting rid of need 
+/* Thread context. With it we are getting rid of need
  * to call the heavy TSRMLS_FETCH() */
 #ifdef ZTS
 # define PHP_EVENT_COMMON_THREAD_CTX void ***thread_ctx
 	PHP_EVENT_OBJECT_HEAD;
 
 	struct bufferevent    *bevent;
+        int                   _internal;
 	zval                  *self;        /* Object itself. For callbacks                   */
 	zval                  *data;        /* User custom data                               */
 	zval                  *input;       /* Input buffer */