Skip to content

Instantly share code, notes, and snippets.

View cpq's full-sized avatar
🎭
Купатися чи не купатись?

Sergey Lyubka cpq

🎭
Купатися чи не купатись?
View GitHub Profile
@cpq
cpq / callbacks.c
Created October 16, 2014 17:08
Callback APIs
// Task: user needs to set up a simple HTTP server
// Option 1
static void my_handler(struct ns_connection *nc, int ev, void *ev_data) {
switch (ev) {
case NS_HTTP_REQUEST:
mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n hi there!! ");
nc->flags |= NSF_FINISHED_SENDING_DATA; // Schedule connection close
break;
default: // We can catch low-level events like NS_RECV, etc
@cpq
cpq / diagrams.adoc
Created November 29, 2014 08:25
asciidoc diagrams

Graphviz

digraph g {
    a -> b
    b -> c
    c -> d
    d -> a
}
@cpq
cpq / producer_consumer.c
Created December 28, 2014 20:04
One-to-many producer/consumer implementation on top of POSIX API
struct task {
socket_t accepted_socket;
};
struct queue {
struct task tasks[NUM_WORKER_THREADS];
int num_tasks;
pthread_mutex_t mutex;
pthread_cond_t condvar;
};
@cpq
cpq / restore_mac_trash.pl
Created January 22, 2015 10:41
MacOS: restore all files from Trash folder to their original location
#!/usr/bin/env perl
# Copyright (c) 2015 Sergey Lyubka
# All rights reserved
use Encode;
my $dir = "/Users/$ENV{USER}/.Trash";
sub read_file($) { local $/; open FD, $_[0] or die $_[0]; binmode FD; <FD>; }
@cpq
cpq / dash_editor.md
Last active August 29, 2015 17:25
Dashboard editor

DASHBOARD EDITOR

Overview

Smart.js IoT platform provides two components for building private smart device infrastructures:

  • Firmware for the embedded devices, which is capable of harvesting
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>GitHub Stars</title>
</head>
<body>
hi
</body>
</html>
@cpq
cpq / overflow.c
Created April 4, 2016 12:33
Simple web server with buffer overflow vulnerability
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define LISTENING_PORT 8000
static void serve_new_connection(FILE *fp) {
char method[10], uri[100], protocol[20];
@cpq
cpq / mess.c
Last active May 27, 2016 15:13
Marshall arbitrary C structures into JSON
// $ cc -W -Wall ~/tmp/mess.c && ./a.out
// {"a": 9, "c": "i am bar", "d": [{"a": 3, "b": null}, {"a": 7, "b": "oops"}]}
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "mess.h"
#define STRUCT_NAME foo
#define STRUCT_FIELDS ATTR(int, a) ATTR(char *, b)
@cpq
cpq / package.json
Last active August 18, 2016 13:48
web server skeleton
{
"name": "web_server",
"description": "web server skeleton",
"version": "1.0.0",
"private": false,
"dependencies": {
"express": "3.x",
"sqlite3": "*",
"minimist": "*",
"supervisor": "*",
@cpq
cpq / ws.js
Last active December 2, 2016 19:52
WebSocket client
var reconnect = function() {
var url = 'ws://' + location.host + '/ws';
ws = new WebSocket(url);
ws.onopen = function(ev) { console.log(ev); };
ws.onclose = function(ev) { console.log(ev); setTimeout(reconnect, 1000); };
ws.onmessage = function(ev) { console.log('message', ev); };
ws.onerror = function(ev) { console.log('error', ev); ws.close(); };
};