Skip to content

Instantly share code, notes, and snippets.

@ArtemGr
ArtemGr / makefile
Last active February 15, 2024 14:29
SPI helper for a PostgreSQL CHECK when using one-to-many with a jsonb.
all: spi.so
spi.so: spi.o makefile
g++ -shared -o spi.so spi.o
cp --remove-destination spi.so /var/lib/postgresql/spi.so
spi.o: spi.cc makefile
g++ -g -O2 -Wall -std=c++11 -fpic -c -o spi.o -I/usr/include/postgresql -I/usr/include/postgresql/9.4/server spi.cc
clean:
@ArtemGr
ArtemGr / system.rs
Last active December 22, 2023 07:08
Read lines from a pipe as soon as they come out (useful for filtering).
#![feature(mpsc_select, box_syntax)]
use std::io;
use std::process::Command;
use std::sync::mpsc::{channel, Receiver, Select};
use std::string::FromUtf8Error;
use std::thread::spawn;
#[derive(Debug)]
enum PipeError {
@ArtemGr
ArtemGr / BsonJson.cpp
Last active December 21, 2023 08:52
BSON-JSON with C++ and Rapidjson
#include <mongo/bson/bson.h>
using mongo::BSONObj; using mongo::BSONObjBuilder;
using mongo::BSONArray; using mongo::BSONArrayBuilder;
using mongo::BSONElement;
#include <rapidjson/document.h>
#include <rapidjson/reader.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
@ArtemGr
ArtemGr / main.rs
Created November 21, 2023 02:47
bit get set
pub fn main() {
let mut hot = 0u128;
for ix in [0, 127] {hot |= 1 << ix}
println! ("{:b}", hot);
assert! (hot & (1 << 0) != 0);
assert! (hot & (1 << 126) == 0);
assert! (hot & (1 << 127) != 0);
}
@ArtemGr
ArtemGr / proxy_issue_43.cc
Created October 18, 2012 12:39
tracking states to workaround issue 43 in libevhtp
#ifdef __CYGWIN__ // hide "__transaction_atomic" from Eclipse
# define STM
#else
// http://gcc.gnu.org/wiki/TransactionalMemory
# define STM __transaction_atomic
#endif
__thread struct event_base* EvServ::_THR_EVBASE = nullptr;
// evhtp threads need separate evdns to make http requests.
@ArtemGr
ArtemGr / main.rs
Last active July 9, 2023 19:49
serde f16 73871891
extern crate half; // 2.3.1
extern crate serde; // 1.0.164
extern crate serde_json; // 1.0.99
use half::f16;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
fn f16ser<S>(fv: &f16, se: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ArtemGr
ArtemGr / index.js
Last active May 17, 2023 05:18
llog.js
// To add this Gist to a project:
//
// npm install https://gist.github.com/25fad725870b0ada74df2bce7d115cc1.git
/**
* @param {number} level
*/
exports.trace = function (level) {
const obj = {}
const lim = Error.stackTraceLimit
@ArtemGr
ArtemGr / example.sh
Last active April 10, 2023 18:59
Testing HTTP pipelining from the command line.
# http://aaronhawley.livejournal.com/12621.html
(echo -en "GET / HTTP/1.1\nHost: fropl.com\n\nGET / HTTP/1.1\nHost: fropl.com\n\n"; sleep 0.1) | telnet localhost 80
# Also:
perl -e '$| = 1; print "GET / HTTP/1.1\nHost: fropl.com\n\nGET / HTTP/1.1\nHost: fropl.com\n\n"; sleep (1)' | telnet localhost 80
# Also (from https://github.com/ellzey/libevhtp/issues/86#issuecomment-19137572):
(echo -en "GET /1 HTTP/1.1\r\n\r\nGET /2 HTTP/1.1\r\n\r\n"; sleep 0.1) | nc localhost 8081
@ArtemGr
ArtemGr / .gitignore
Last active November 2, 2022 10:59
llog
/__pycache__
/llog
@ArtemGr
ArtemGr / verify.rs
Created February 25, 2017 19:26
Using OpenSSL to verify the JWT RS256 signature in Rust.
use openssl::sign::Verifier;
use openssl::rsa::Rsa;
use openssl::pkey::PKey;
use openssl::hash::MessageDigest;
use serde_json::{self as json, Value as Json};
pub fn firebase_id_token (headers: BTreeMap<&str, &str>, mut stream: &mut BufStream<TcpStream>) -> Result<(), String> {
#[derive(Deserialize, Debug)]
struct Post {firebase_id_token: String}