static int output_body(request_rec *r, SV *body) { dTHX; int rc, type; switch (type = SvTYPE(SvRV(body))) { case SVt_PVAV: rc = output_body_ary(r, (AV *) SvRV(body)); break; case SVt_PVGV: /* TODO: * It's possible to get fd by PerlIO_fileno(IoIFP(sv_2io(body))) * It's possible to get apr_file_t by apr_os_file_put * Is it possible to implement above portable? */ require_pv("IO/Handle.pm"); case SVt_PVMG: if (respond_to(body, "path")) { rc = output_body_path(r, body); if (rc != DECLINED) break; } rc = output_body_obj(r, body, type); break; default: server_error(r, "response body must be an array reference or object"); rc = HTTP_INTERNAL_SERVER_ERROR; break; } return rc; }
static int ipaccess_bcast(struct osmo_fd *fd, unsigned int what) { uint8_t data[2048]; char src[INET_ADDRSTRLEN]; struct sockaddr_in addr = {}; socklen_t len = sizeof(addr); int rc; rc = recvfrom(fd->fd, data, sizeof(data), 0, (struct sockaddr *) &addr, &len); if (rc <= 0) { LOGP(DFIND, LOGL_ERROR, "Failed to read from socket errno(%d)\n", errno); return -1; } LOGP(DFIND, LOGL_DEBUG, "Received request from: %s size %d\n", inet_ntop(AF_INET, &addr.sin_addr, src, sizeof(src)), rc); if (rc < 6) return 0; if (data[2] != IPAC_PROTO_IPACCESS || data[4] != IPAC_MSGT_ID_GET) return 0; respond_to(&addr, fd, data + 6, rc - 6); return 0; }
static void service(FILE *in, FILE *out, char *docroot) { struct HTTPRequest *req; req = read_request(in); respond_to(req, out, docroot); free_request(req); }
only_one_pdu_from_master() { respond_to( 37, valid_connection_request_pdu ); add_connection_event_respond( { 1, 0 } ); run(); }
/** * @test until now, the link layer should respond with an empty response */ BOOST_FIXTURE_TEST_CASE( empty_reponds_to_a_scan_request, advertising_and_connect ) { #if 0 respond_to( 37, // channel { 0x03, 0x0C, // header 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // scanner address 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // advertiser address } ); run(); static const std::vector< std::uint8_t > expected_response = { }; find_schedulting( []( const test::schedule_data& pdu ) -> bool { return pdu.channel == 37 && pdu.transmision_time.zero() && pdu.transmitted_data == expected_response; }, "empty_reponds_to_a_scan_request" ); #endif }
BOOST_FIXTURE_TEST_CASE( connection_details_reported_when_connection_is_established, link_layer_only_connect_callback ) { respond_to( 37, { 0xc5, 0x22, // header 0x3c, 0x1c, 0x62, 0x92, 0xf0, 0x48, // InitA: 48:f0:92:62:1c:3c (random) 0x47, 0x11, 0x08, 0x15, 0x0f, 0xc0, // AdvA: c0:0f:15:08:11:47 (random) 0x5a, 0xb3, 0x9a, 0xaf, // Access Address 0x08, 0x81, 0xf6, // CRC Init 0x03, // transmit window size 0x0b, 0x00, // window offset 0x18, 0x00, // interval (30ms) 0x02, 0x00, // slave latency 0x48, 0x05, // connection timeout 0xf3, 0x5f, 0x1f, 0x7f, 0x1f, // used channel map 0xaa // hop increment and sleep clock accuracy (10 and 50ppm) } ); add_connection_event_respond( { 0, 1 } ); run( 2 ); const auto reported_details = only_connect_callback.reported_details; static const std::uint8_t map_data[] = { 0xf3, 0x5f, 0x1f, 0x7f, 0x1f }; bluetoe::link_layer::channel_map channels; channels.reset( &map_data[ 0 ], 10 ); BOOST_CHECK( equal( reported_details.channels(), channels ) ); BOOST_CHECK_EQUAL( reported_details.interval(), 0x18 ); BOOST_CHECK_EQUAL( reported_details.latency(), 2 ); BOOST_CHECK_EQUAL( reported_details.timeout(), 0x548 ); BOOST_CHECK_EQUAL( reported_details.cumulated_sleep_clock_accuracy_ppm(), 50 + 100 ); }
BOOST_FIXTURE_TEST_CASE( connection_is_not_established_before_the_first_connection_event, link_layer_only_connect_callback ) { BOOST_CHECK( !only_connect_callback.connection_established_called ); respond_to( 37, valid_connection_request_pdu ); run(); BOOST_CHECK( !only_connect_callback.connection_established_called ); }
BOOST_FIXTURE_TEST_CASE( connection_lost_by_timeout, link_layer_only_disconnect_callback ) { BOOST_CHECK( !only_disconnect_callback.only_disconnect_called ); respond_to( 37, valid_connection_request_pdu ); run( 2 ); BOOST_CHECK( only_disconnect_callback.only_disconnect_called ); }
BOOST_FIXTURE_TEST_CASE( addresses_reported_when_connection_established, link_layer_only_connect_callback ) { respond_to( 37, valid_connection_request_pdu ); add_connection_event_respond( { 0, 1 } ); run(); const auto reported_addresses = only_connect_callback.reported_addresses; BOOST_CHECK_EQUAL( reported_addresses.remote_address(), bluetoe::link_layer::address( { 0x3c, 0x1c, 0x62, 0x92, 0xf0, 0x48 } ) ); BOOST_CHECK_EQUAL( reported_addresses.local_address(), bluetoe::link_layer::address( { 0x47, 0x11, 0x08, 0x15, 0x0f, 0xc0 } ) ); }
BOOST_FIXTURE_TEST_CASE( connection_update_not_called_by_default, link_layer_only_changed_callback ) { BOOST_CHECK( !only_changed_callback.only_changed_called ); respond_to( 37, valid_connection_request_pdu ); add_connection_event_respond( { 0, 1 } ); run(); BOOST_CHECK( !only_changed_callback.only_changed_called ); }
BOOST_FIXTURE_TEST_CASE( connection_not_lost, link_layer_only_disconnect_callback ) { BOOST_CHECK( !only_disconnect_callback.only_disconnect_called ); respond_to( 37, valid_connection_request_pdu ); ll_empty_pdus( 120 ); run( 3 ); BOOST_CHECK( !only_disconnect_callback.only_disconnect_called ); }
BOOST_FIXTURE_TEST_CASE( connection_update, link_layer_only_changed_callback ) { BOOST_CHECK( !only_changed_callback.only_changed_called ); respond_to( 37, valid_connection_request_pdu ); add_connection_update_request( 5, 6, 40, 1, 25, 2 ); ll_empty_pdus( 120 ); run( 3u ); BOOST_CHECK( only_changed_callback.only_changed_called ); }
BOOST_FIXTURE_TEST_CASE( connection_is_established_callback_called_only_once, link_layer_only_connect_callback ) { BOOST_CHECK( !only_connect_callback.connection_established_called ); respond_to( 37, valid_connection_request_pdu ); add_connection_event_respond( { 0, 1 } ); add_connection_event_respond( { 0, 1 } ); run(); BOOST_CHECK( only_connect_callback.connection_established_called ); only_connect_callback.connection_established_called = false; run(); BOOST_CHECK( !only_connect_callback.connection_established_called ); }
static int output_body_obj(request_rec *r, SV *obj, int type) { dTHX; SV *buf_sv; apr_off_t clen = 0; STRLEN len; dSP; char *buf; int count; if (type == SVt_PVMG && !respond_to(obj, "getline")) { server_error(r, "response body object must be able to getline"); return HTTP_INTERNAL_SERVER_ERROR; } ENTER; SAVETMPS; SAVESPTR(PL_rs); PL_rs = newRV_inc(newSViv(AP_IOBUFSIZE)); while (1) { PUSHMARK(SP); XPUSHs(obj); PUTBACK; count = call_method("getline", G_SCALAR); if (count != 1) croak("Big trouble\n"); SPAGAIN; buf_sv = POPs; if (SvOK(buf_sv)) { buf = SvPV(buf_sv, len); clen += len; ap_rwrite(buf, len, r); } else { break; } } if (clen > 0) { ap_set_content_length(r, clen); } PUSHMARK(SP); XPUSHs(obj); PUTBACK; call_method("close", G_DISCARD); SPAGAIN; PUTBACK; FREETMPS; LEAVE; return OK; }
BOOST_FIXTURE_TEST_CASE( connection_lost_by_disconnect, link_layer_only_disconnect_callback ) { BOOST_CHECK( !only_disconnect_callback.only_disconnect_called ); respond_to( 37, valid_connection_request_pdu ); ll_empty_pdus( 3 ); add_connection_event_respond( { 0x03, 0x02, 0x02, 0x12 } ); run( 2 ); BOOST_CHECK( only_disconnect_callback.only_disconnect_called ); }
static void service ( FILE *in, /* input fd */ FILE *out, /* output fd */ char *docroot /* docroot path */ ) { struct HTTPRequest *req = NULL; dbg( "in=%p, out=%p, docroot=%p\n", in, out, docroot ); req = read_request( in ); respond_to( req, out, docroot ); free_request( req ); }
BOOST_FIXTURE_TEST_CASE( connection_details_reported_when_connection_is_updates, link_layer_only_changed_callback ) { respond_to( 37, valid_connection_request_pdu ); add_connection_update_request( 5, 6, 40, 1, 25, 2 ); ll_empty_pdus( 120 ); run( 3u ); static const std::uint8_t map_data[] = { 0xff, 0xff, 0xff, 0xff, 0x1f }; bluetoe::link_layer::channel_map channels; channels.reset( &map_data[ 0 ], 10 ); const auto reported_details = only_changed_callback.reported_details; BOOST_CHECK( equal( reported_details.channels(), channels ) ); BOOST_CHECK_EQUAL( reported_details.interval(), 40 ); BOOST_CHECK_EQUAL( reported_details.latency(), 1 ); BOOST_CHECK_EQUAL( reported_details.timeout(), 25 ); BOOST_CHECK_EQUAL( reported_details.cumulated_sleep_clock_accuracy_ppm(), 50 + 100 ); }
void* client_thread(void * client_queue) { int confd; status res; char* message = NULL; queue *cq = (queue *)client_queue; for(;;) { confd = dequeue(cq); printf("Connection %d\n acquired by thread %lu\n", confd, pthread_self()); /* Serve client until they close connection, or * there is an error when attempting to read/write to/from them */ do { /* Obtain the request message as a string ending in '\r\n\r\n */ if ((res = obtain_request(&message, confd)) != OK) { printf("Failed to obtain HTTP request from connection %d\n",confd); if (res != CONNECTION_ERROR) { res = send_error(res, confd); } } else { /* If request message recieved successfully */ res = respond_to(message, confd); } } while (res != CONNECTION_ERROR); printf("Closing connection %d for thread %lu\n",confd, pthread_self()); close(confd); } /* Should never get here, but if it does * might as well attempt to avoid any memory leaks */ free(message); return NULL; }
BOOST_FIXTURE_TEST_CASE( addresses_reported_when_connection_established, link_layer_only_connect_callback ) { respond_to( 37, { 0x85, 0x22, // header 0x3c, 0x1c, 0x62, 0x92, 0xf0, 0x48, // InitA: 48:f0:92:62:1c:3c (public) 0x47, 0x11, 0x08, 0x15, 0x0f, 0xc0, // AdvA: c0:0f:15:08:11:47 (random) 0x5a, 0xb3, 0x9a, 0xaf, // Access Address 0x08, 0x81, 0xf6, // CRC Init 0x03, // transmit window size 0x0b, 0x00, // window offset 0x18, 0x00, // interval (30ms) 0x00, 0x00, // slave latency 0x48, 0x00, // connection timeout (720ms) 0xff, 0xff, 0xff, 0xff, 0x1f, // used channel map 0xaa // hop increment and sleep clock accuracy (10 and 50ppm) } ); add_connection_event_respond( { 0, 1 } ); run(); const auto reported_addresses = only_connect_callback.reported_addresses; BOOST_CHECK_EQUAL( reported_addresses.remote_address(), bluetoe::link_layer::public_device_address( { 0x3c, 0x1c, 0x62, 0x92, 0xf0, 0x48 } ) ); BOOST_CHECK_EQUAL( reported_addresses.local_address(), bluetoe::link_layer::random_device_address( { 0x47, 0x11, 0x08, 0x15, 0x0f, 0xc0 } ) ); }
bool rb_vm_respond_to2(VALUE obj, VALUE klass, SEL sel, bool priv, bool check_override) { return respond_to(obj, klass, sel, priv, check_override); }
template<typename T> Method *respond_to(value_t obj, T &&name) { return respond_to(obj, symbol_cast(std::forward<T>(name))); }
bool rb_vm_respond_to(VALUE obj, SEL sel, bool priv) { return respond_to(obj, Qnil, sel, priv, true); }