海运的博客

The plain HTTP request was sent to HTTPS port

发布时间:December 6, 2014 // 分类:Nginx // No Comments

Nginx配置80和443端口在同一server段时不要这样设置:

listen       80;
listen       443;
ssl on;

应该在443端口后添加ssl:

listen       80;
listen       443 ssl;

有时后端PHP要判断HTTPS:

fastcgi_param HTTPS $https if_not_empty;

PHP Libevent HTTP客户端

发布时间:December 4, 2014 // 分类:PHP // No Comments

<?php
//请求完成回调
function _request_handler($req, $base) {
  global $pend_req;
  //echo __FUNCTION__, PHP_EOL;

  if (is_null($req)) {
    //echo "Timed out\n";
  } else {
    $response_code = $req->getResponseCode();

    if ($response_code == 0) {
      //echo "Connection refused\n";
    } elseif ($response_code != 200) {
      //echo "Unexpected response: $response_code\n";
    } else {
      //echo "Success: $response_code\n";
      /*
      $buf = $req->getInputBuffer();
      echo "Body:\n";
      while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
      echo $s, PHP_EOL;
      }
       */
    }
  }
  $pend_req--;
  //退出循环
  if (!$pend_req) {
    $base = $conn->getBase();
    $base->exit(NULL);
  }
  //释放内存
  unset($req);
  unset($conn);
}

//$address = "www.google.com";
$pend_req = 0;
$port = 80;
//初始化event base
$base = new EventBase();
echo "Event method used: ", $base->getMethod(), PHP_EOL;
 //使用异步DNS
$dns_base = new EventDnsBase($base, TRUE);
$f= fopen("./50000.txt","r");
while (!feof($f))
{
  $line = fgets($f);
  //echo $address;
  $address = trim($line);
  //新建http连接事件到base
  $conn = new EventHttpConnection($base, $dns_base, $address, $port);
  $conn->setTimeout(1);
  //设置请求回调
  $req = new EventHttpRequest("_request_handler", $conn);

  $req->addHeader("Host", $address, EventHttpRequest::OUTPUT_HEADER);
  $req->addHeader("Content-Length", "0", EventHttpRequest::OUTPUT_HEADER);
  $conn->makeRequest($req, EventHttpRequest::CMD_GET, "/");
  $pend_req++;
}
fclose($f);
//事件主循环
$base->loop();
?>

c语言版,参考:https://github.com/libevent/libevent/issues/115

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <evhttp.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/bufferevent.h>
typedef struct my_struct_s my_struct_t;

struct my_struct_s {
  struct evhttp_connection *conn;
  struct evhttp_request *req;
  struct evhttp_uri *uri;
  struct event *cleanup;
};

struct event_base *Base_Primary;

char *trimwhitespace(char *str)
{
  char *end;

  // Trim leading space
  while(isspace(*str)) str++;

  if(*str == 0)  // All spaces?
    return str;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;

  // Write new null terminator
  *(end+1) = 0;

  return str;
}

void connection_free(int sock, short which, void *arg) {
  //printf("freeing connection!!! The socket's FD would have been closed when the HTTP request ended and the ->req object would have been free'd\n");

  // Get our structure object
  my_struct_t *myStruct = arg;

  // Cleanup our properties
  event_free(myStruct->cleanup);
  evhttp_connection_free(myStruct->conn);
  evhttp_request_free(myStruct->req);
  evhttp_uri_free(myStruct->uri);

  // Free our custom structure
  free(myStruct);
}

void http_request_done(struct evhttp_request *req, void *arg){

  // Get our custom struct
  my_struct_t *myStruct = arg;

  // Setup our timeout information (we delay 5 seconds)
  struct timeval Timeout;
  Timeout.tv_sec = 0;
  Timeout.tv_usec = 0;

  // Add this structure to our cleanup base to be cleaned up synchronously
  // TODO: Probably not the best way to cleanup and event, but it'l work for the purposes of illustration.
  // This way would ensure no race conditions exist, but it's probably not the most efficient depending on how many requests, etc we're dealing with.
  myStruct->cleanup = evtimer_new(Base_Primary, connection_free, (void *)myStruct);
  evtimer_add(myStruct->cleanup, &Timeout);

  //printf("http_request_done, we put our custom strucutre into a cleanup event to be freed!\n");
}

int http_req(char *uri) {

  // Allocate our custom struture
  my_struct_t *myStruct = malloc(sizeof(my_struct_t));

  // Create our EVHTP connection and request
  myStruct->uri = evhttp_uri_parse(uri);
  myStruct->conn = evhttp_connection_base_new(Base_Primary, NULL, uri, 80);
  myStruct->req = evhttp_request_new(http_request_done, myStruct);
  evhttp_add_header(evhttp_request_get_output_headers(myStruct->req), "Host", "localhost");
  evhttp_add_header(evhttp_request_get_output_headers(myStruct->req), "Connection", "close");
  evhttp_make_request(myStruct->conn, myStruct->req, EVHTTP_REQ_GET, uri);
  evhttp_connection_set_timeout(myStruct->req->evcon, 2);
  return 1;
}


// Define our primary function
int main(int argc, char *argv[]) {

  // Initialize our bases
  Base_Primary = event_base_new();

  char filename[] = "/tmp/50000.txt"; //文件名
  FILE *fp; 
  char StrLine[1024];             //每行最大读取的字符数
  char *host;
  if((fp = fopen(filename,"r")) == NULL) //判断文件是否存在及可读
  { 
    printf("error!"); 
    return -1; 
  } 

  while (!feof(fp)) 
  { 
    fgets(StrLine,1024,fp);  //读取一行
    host = StrLine;
    host = trimwhitespace(host);
    //printf("%s", host); //输出
    http_req(host);
  }
  fclose(fp);  

  //
  //event_base_loop(Base_Primary);
  event_base_dispatch(Base_Primary);

  // Free our primary base
  event_base_free(Base_Primary);
  return 1;
}

DNS服务器获取用户IP

发布时间:November 20, 2014 // 分类:DNS // No Comments

在做智能DNS的时候全长VIEW根据请求的IP返回不同的IP,请求的IP是递归DNS的IP,edns-client-subnet附加客户端IP。

PHP JSON解码GBK

发布时间:November 19, 2014 // 分类:PHP // No Comments

在用PHP JSON处理符合标准的GBK编码内容时会返回null,将内容从GBK转换为UTF8再处理正常:

$jsonp = mb_convert_encoding($jsonp, "gbk", "utf-8");
//或
$jsonp = iconv('gbk','utf-8',$jsonp)

跨语言/平台调用

发布时间:November 17, 2014 // 分类: // No Comments

本机跨语言调用可使用IPC通信,跨平台/网络可使用以下:
轻量级:
http://www.phprpc.org/zh_CN/
https://github.com/hprose
重量级:
https://thrift.apache.org/
https://github.com/google/protobuf/

分类
最新文章
最近回复
  • liyk: 这个方法获取的IPv6大概20分钟之后就会失效,默认路由先消失,然后Global IPV6再消失
  • 海运: 不好意思,没有。
  • zongboa: 您好,請問一下有immortalwrt設定guest Wi-Fi的GUI教學嗎?感謝您。
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
归档