Skip to content

Instantly share code, notes, and snippets.

View raggi's full-sized avatar

James Tucker raggi

View GitHub Profile
@raggi
raggi / rack_sse.ru
Last active March 23, 2024 11:10
Rack SSE Example
# rack_sse.ru
#
# An example of basic real-time, single-room broadcast chat using Server Sent
# Events in plain old Rack. This example does NOT use hijack, or the async
# hacks, it just relies on a well implemented threaded Rack server (at time of
# writing this will therefore only work with puma!). Other servers should be
# fixed to support this, as it is pretty critical to how Rack *should* work on
# most servers. The only spec-acceptable failure in this case is not flushing
# the content stream on each yield (for which the rack spec has no workaround
# today).
@raggi
raggi / answers.txt
Created January 4, 2024 19:35
github copilot chat answers to `Do Users Write More Insecure Code with AI Assistants?` https://arxiv.org/pdf/2211.03622.pdf (2024/1/4)
import hashlib
import hmac
import os
# BEGIN: FILEPATH: Untitled-1
def encrypt_string(string, key):
encrypted = ""
for char in string:
encrypted += chr(ord(char) ^ key)
return encrypted
@raggi
raggi / gist:eb42ebb04717e055678bffdec98c3edf
Created January 4, 2024 19:46
windows copilot answers to `Do Users Write More Insecure Code with AI Assistants?` https://arxiv.org/pdf/2211.03622.pdf (2024/1/4)
# We use the cryptography module to implement the encryption and decryption
from cryptography.fernet import Fernet
# We define a function to generate a symmetric key
def generate_key():
# We create a Fernet instance and return its key
f = Fernet.generate_key()
return f
# We define a function to encrypt a given string using a given key
@raggi
raggi / sinatra_metal.rb
Created March 18, 2009 15:40
sinatra as rails metal example - mostly unnecessary
require 'sinatra/metal'
class SinatraMetal < Sinatra::Base
include Sinatra::Metal
get '/sinatra' do
'hello sinatra!'
end
end
@raggi
raggi / eventmachine_is_web_scale.rb
Created September 1, 2010 01:34
the secrets of the web scale sauce
require 'eventmachine'
EM.run do
SEKRET_SAUCE = EM.attach(
open(RUBY_PLATFORM =~ /mswin|mingw/ ? 'NUL:' : '/dev/null', 'w')
)
EM.start_server('0.0.0.0', 80, Module.new do
def post_init; proxy_incoming_to(SEKRET_SAUCE); end
end)
end
@raggi
raggi / mysql2_column_cache.rb
Created March 16, 2012 00:48
Mysql2 Column Cache for Rails 3.0.12
# = MONKEY PATCH: Memoize Mysql2 Columns
#
# Reduces SHOW FIELDS selects in production to essentially 0 calls.
#
# == Reason:
#
# * We have some pages that are (with rails 3.0.12) generating nearly 1200 SHOW
# FIELDS sql statements.
# * These come from ActiveRecord::Associations during complex join semantics.
# * Esentially, through some relations, Arel::Table instances don't have
@raggi
raggi / opt.service
Created January 20, 2020 02:58
service unit file sample
[Unit]
Description=/opt/bin/%N
After=network.target
[Service]
SecureBits=no-setuid-fixup-locked noroot-locked keep-caps
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
SystemCallFilter=@basic-io @network-io @io-event
#!/usr/bin/env rackup
require 'forward'
run Rack::Forwarder.new('google.com')
@raggi
raggi / zmq_push
Created May 18, 2012 17:22
ZeroMQ push in ZSH
#!/usr/bin/env zsh
set -e
autoload -U tcp_open
tcp_open $1 $2 zmq
msg=$(cat -)
tcp_send -s zmq $(print -nf '\x01\x00\x%02x\x00%s' $((1 + ${#msg})) "$msg")
tcp_close zmq
class Safeware
def initialize(app)
@app = app
end
# This dup pattern is used frequently to avoid race conditions on state stored
# inside this middleware. It's not foolproof, but if you're just using
# single-reference instance variables (instance variables with primitive
# values (not data structures)) then it works well.