/** * Take the request header and adds it to a magic GUID. * Which is returned in a SHA1-hash * @param req - Mongrel2 request data * @return 0 on success */ static int mongrel2_ws_08_calculate_accept(mongrel2_request *req, bstring *ptr){ int retval; bstring key = mongrel2_request_get_header(req,WEBSOCKET_08_KEY); if(key == NULL){ return -1; } retval = bcatcstr(key,WEBSOCKET_08_GUID); if(retval != 0){ return -1; } void* buf = calloc(sizeof(char),SHA1_LEN); if(buf == NULL){ return -1; } sha1((const unsigned char*)bdata(key),blength(key),buf); bstring accept = blk2bstr(buf,SHA1_LEN); bstring accept_encoded = bBase64Encode(accept); bdestroy(accept); free(buf); bdestroy(key); *ptr = accept_encoded; return 0; }
int Connection_deliver(Connection *conn, bstring buf) { int rc = 0; bstring b64_buf = bBase64Encode(buf); rc = IOBuf_send(conn->iob, bdata(b64_buf), blength(b64_buf)+1); check_debug(rc == blength(b64_buf)+1, "Failed to write entire message to conn %d", IOBuf_fd(conn->iob)); bdestroy(b64_buf); return 0; error: bdestroy(b64_buf); return -1; }
int Connection_deliver(Connection *conn, bstring buf) { int rc = 0; bstring b64_buf = bBase64Encode(buf); check(b64_buf != NULL, "Failed to base64 encode data."); check(conn->iob != NULL, "There's no IOBuffer to send to, Tell Zed."); /* The deliver task will free the buffer */ rc = Connection_deliver_enqueue(conn,b64_buf); check_debug(rc == 0, "Failed to write message to conn %d", IOBuf_fd(conn->iob)); return 0; error: bdestroy(b64_buf); return -1; }
int test9 (void) { struct tagbstring t = bsStatic ("Hello world"); bstring b, c; int err, ret = 0; printf ("TEST: Base 64 codec.\n"); b = bBase64Encode (&t); ret += 0 >= biseqcstr (b, "SGVsbG8gd29ybGQ="); c = bBase64DecodeEx (b, &err); ret += 0 != err; ret += 0 >= biseq (c, &t); bdestroy (b); bdestroy (c); printf ("\t# failures: %d\n", ret); return ret; }
END_TEST START_TEST(core_009) { struct tagbstring t = bsStatic("Hello world"); int err, ret = 0; bstring b, c; b = bBase64Encode(&t); ck_assert(b != NULL); ret += 0 >= biseqcstr(b, "SGVsbG8gd29ybGQ="); c = bBase64DecodeEx(b, &err); ck_assert(b != NULL); ck_assert_int_eq(err, 0); ret += 0 >= biseq(c, &t); ck_assert_int_eq(ret, 0); ret = bdestroy(b); ck_assert_int_eq(ret, BSTR_OK); ret = bdestroy(c); ck_assert_int_eq(ret, BSTR_OK); }