Skip to content

Instantly share code, notes, and snippets.

@quant61
Last active March 4, 2021 17:16
Show Gist options
  • Save quant61/eb97d8dc98c476239210a566c275c571 to your computer and use it in GitHub Desktop.
Save quant61/eb97d8dc98c476239210a566c275c571 to your computer and use it in GitHub Desktop.
functions
// protects from directory traversal
func safeJoin(baseDir, path string) string{
return filepath.Join(baseDir, filepath.Join("/", path))
}
func getSyscallArg(v interface{}) uintptr {
r := reflect.ValueOf(v)
switch r.Kind(){
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return uintptr(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return uintptr(r.Uint())
case reflect.Ptr, reflect.UnsafePointer, reflect.Slice:
return r.Pointer()
case reflect.String:
b := []byte(r.String())
b = append(b, 0)
return reflect.ValueOf(b).Pointer()
// TODO: add&test support Array and Struct
//case reflect.Array:
// return r.Index(0).Addr()
}
// or panic?
return 0
}
func getByte(addr uintptr) byte{
return (*struct{b byte})(unsafe.Pointer(addr)).b
}
func setByte(addr uintptr, value byte){
(*struct{b byte})(unsafe.Pointer(addr)).b = value
}
function arrayToUnique(array){
return [...new Set(array)]
}
// promise version of https://stackoverflow.com/a/14269536/6076531
function streamToBuffer(stream) {
return new Promise(function(resolve, reject) {
let bufs = [];
stream.on('data', function (d) {
// console.log('download', Buffer.concat(bufs).byteLength);
bufs.push(d);
});
stream.on('end', function() {
console.log('stream loaded');
resolve(Buffer.concat(bufs));
});
stream.on('error', err => reject(err));
});
}
function requestsStack(requests, results = []) {
if(requests.length === 0){
return results;
}
let [current, ...rest] = requests;
if(!Array.isArray(current)){
current = [current];
}
console.log(`start request ${current}`);
return connection.query(...current).then(function (data) {
console.log(`request ${current} done`);
results.push(data);
return requestsStack(rest, results);
});
}
let hasDupes = array => new Set(array).size !== array.length
let getDupsCount = array => array.length - new Set(array).size
<?php
/**
* [{id:13}, {id:42}] => {13: {id:13}, 42:{id:42}}
*
* @param array $items
* @param callable $by
* @return array
*/
function makeMapByFunc(array $items, $by){
$map = [];
foreach ($items as $item){
$map[$by($item)] = $item;
}
return $map;
}
/**
* TOGIST
* @param $source
* @param $target
* @param $keys
*/
function copyKeys($source, &$target, $keys)
{
foreach ($keys as $key){
if(array_key_exists($key, $source)){
$target[$key] = $source[$key];
}
}
}
function get(&$var, $default=null) {
return isset($var) ? $var : $default;
}
// version for php5
function makeRequest($url, $config = []){
$ch = curl_init();
if(isset($config['queryParams']) && is_array($config['queryParams'])){
if (strpos($url, '?') === false) {
$url .= '?';
}
$url .= http_build_query($config['queryParams']);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(isset($config['method'])){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $config['method']);
switch ($config['method']){
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
break;
case 'PUT':
curl_setopt($ch, CURLOPT_PUT, 1);
break;
}
}
$headers = isset($config['headers'])? $config['headers'] : [];
if(isset($config['body'])){
curl_setopt($ch, CURLOPT_POSTFIELDS, $config['body']);
} else if(isset($config['json'])){
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($config['json']));
}
if($headers){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
addOption($ch, CURLOPT_USERAGENT, $config, 'userAgent');
addOption($ch, CURLOPT_COOKIE, $config, 'cookies');
addOption($ch, CURLOPT_COOKIEFILE, $config, 'cookieFile');
addOption($ch, CURLOPT_COOKIEJAR, $config, 'cookieJar');
if(isset($config['proxy'])){
$proxy = $config['proxy'];
if (isset($proxy['ip']) && isset($proxy['port'])) {
curl_setopt($ch, CURLOPT_PROXY, $proxy['ip'] . ':' . $proxy['port']);
} elseif (isset($proxy['ip'])) {
curl_setopt($ch, CURLOPT_PROXY, $proxy['ip']);
}
if (isset($proxy['login']) && isset($proxy['password'])){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy['login'] . ':' . $proxy['password']);
}
if (isset($proxy['type'])) {
switch ($proxy['type']){
case 'http':
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
break;
case 'socks4':
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
break;
case 'socks5':
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
}
}
}
if(isset($config['curlOptions'])){
curl_setopt_array($ch, $config['curlOptions']);
}
curl_setopt($ch, CURLOPT_HEADER, 1);
$rawResponse = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($rawResponse, 0, $headerSize);
$content = substr($rawResponse, $headerSize);
$err = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$report = compact('content', 'err', 'info', 'rawResponse', 'headers');
$parsedJson = json_decode($content, true);
if($parsedJson){
$report['parsedJson'] = $parsedJson;
}
return $report;
}
function addOption($ch, $curlOption, $config, $key){
if(isset($config[$key])){
curl_setopt($ch, $curlOption, $config[$key]);
}
}
/**
* possible answer to "php SimpleXMLElement get attributes as associative array"
*
* @param \SimpleXMLElement $element
* @return array
*/
function getSimpleXmlElementAttributes(\SimpleXMLElement $element): array {
$ans = [];
foreach ($element->attributes() as $k => $v){
$ans[$k] = (string)$v;
}
return $ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment