使用Nodejs HTTP Server的自定义HTTP方法

时间:2012-04-30 09:10:09

标签: http node.js networking

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

好的,我可以让GET和HEAD方法正常工作。 但是一旦我尝试使用非标准方法,例如“SYNC”。这是代码:

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

看起来没问题......让我们远程登录,发出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工作的任何想法?

2 个答案:

答案 0 :(得分:10)

编辑:http解析器已经更新和更改了很多。因此,添加自定义HTTP方法并不是那么简单,但感谢节点5.x中的@admix - here's the relevant entry point

    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;
    }

原始回复:

您必须修补节点才能添加任意方法。见the relevant source lines

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;
  }
}

答案 1 :(得分:5)

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

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);