与HTTP的NodeJS服务器自定义HTTP方法(Custom HTTP method with N

2019-06-24 02:16发布

目前正在写在HTTP上的同步机制,我开始建立我的服务器的NodeJS,这看起来像一个很好的解决方案。

这样就OK了,我可以得到GET和工作头的方法。 但只要我尝试用非标准方法,例如“SYNC”。 下面的代码:

var http = require("http");
var server = http.createServer(function (req, res) {
    res.writeHead(200);
    res.end(req.method);
});
server.listen(8080);

看起来好了...让我们的telnet这个,发出GET,然后一个SYNC请求

mylaptop:~ aob$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked

3
GET
0

SYNC / HTTP/1.1
Connection closed by foreign host.

我如何能得到SYNC工作任何想法?

Answer 1:

编辑:HTTP分析器进行了更新和改变了很多。 正因为如此,它不是很直接的添加自定义HTTP方法,但由于@admix - 这里是有关的入口点在节点5.x的:

    switch (ch) {
      case 'A': parser->method = HTTP_ACL; break;
      case 'B': parser->method = HTTP_BIND; break;
      case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
      case 'D': parser->method = HTTP_DELETE; break;
      case 'G': parser->method = HTTP_GET; break;
      case 'H': parser->method = HTTP_HEAD; break;
      case 'L': parser->method = HTTP_LOCK; /* or LINK */ break;
      case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
      case 'N': parser->method = HTTP_NOTIFY; break;
      case 'O': parser->method = HTTP_OPTIONS; break;
      case 'P': parser->method = HTTP_POST;
        /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
        break;
      case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break;
      case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
      case 'T': parser->method = HTTP_TRACE; break;
      case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break;
      default:
        SET_ERRNO(HPE_INVALID_METHOD);
        goto error;
    }

原来的答复:

你将不得不为了添加任意的方法来修补节点。 请参阅相关的源代码行 :

static inline Persistent<String>
method_to_str(unsigned short m) {
  switch (m) {
    case HTTP_DELETE:     return delete_sym;
    case HTTP_GET:        return get_sym;
    case HTTP_HEAD:       return head_sym;
    case HTTP_POST:       return post_sym;
    case HTTP_PUT:        return put_sym;
    case HTTP_CONNECT:    return connect_sym;
    case HTTP_OPTIONS:    return options_sym;
    case HTTP_TRACE:      return trace_sym;
    case HTTP_PATCH:      return patch_sym;
    case HTTP_COPY:       return copy_sym;
    case HTTP_LOCK:       return lock_sym;
    case HTTP_MKCOL:      return mkcol_sym;
    case HTTP_MOVE:       return move_sym;
    case HTTP_PROPFIND:   return propfind_sym;
    case HTTP_PROPPATCH:  return proppatch_sym;
    case HTTP_UNLOCK:     return unlock_sym;
    case HTTP_REPORT:     return report_sym;
    case HTTP_MKACTIVITY: return mkactivity_sym;
    case HTTP_CHECKOUT:   return checkout_sym;
    case HTTP_MERGE:      return merge_sym;
    case HTTP_MSEARCH:    return msearch_sym;
    case HTTP_NOTIFY:     return notify_sym;
    case HTTP_SUBSCRIBE:  return subscribe_sym;
    case HTTP_UNSUBSCRIBE:return unsubscribe_sym;
    default:              return unknown_method_sym;
  }
}


Answer 2:

根据当前版本的NodeJS(5.2.0),你可以在这里找到,你可以自定义HTTP方法:

https://github.com/nodejs/node/blob/master/deps/http_parser/http_parser.c#L961

相关的代码:

parser->method = (enum http_method) 0;
    parser->index = 1;
    switch (ch) {
      case 'A': parser->method = HTTP_ACL; break;
      case 'B': parser->method = HTTP_BIND; break;
      case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break;
      case 'D': parser->method = HTTP_DELETE; break;
      case 'G': parser->method = HTTP_GET; break;
      case 'H': parser->method = HTTP_HEAD; break;
      case 'L': parser->method = HTTP_LOCK; /* or LINK */ break;
      case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break;
      case 'N': parser->method = HTTP_NOTIFY; break;
      case 'O': parser->method = HTTP_OPTIONS; break;
      case 'P': parser->method = HTTP_POST;
        /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
        break;
      case 'R': parser->method = HTTP_REPORT; /* or REBIND */ break;
      case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
      case 'T': parser->method = HTTP_TRACE; break;
      case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break;
      default:
        SET_ERRNO(HPE_INVALID_METHOD);
        goto error;
    }
    UPDATE_STATE(s_req_method);


文章来源: Custom HTTP method with Nodejs HTTP Server