使用erlang编写一个http接口处理代码,方法为post、get、delete,监听端口为12500,post仅能够上传JPG类型的文件,上传其余类型报错500;get能够通过文件名获取文件,文件不存在返回报错404;delete可以删除上传的文件
时间: 2024-02-24 16:58:35 浏览: 160
http接口通用调试工具,支持get、post、put、patch、delete方法
5星 · 资源好评率100%
以下是使用 Erlang 编写的一个简单的 HTTP 服务器,可以处理指定要求:
```erlang
-module(http_server).
-behaviour(gen_server).
-export([start/0, stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_listener/1, accept/1]).
start() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() ->
gen_server:cast(?MODULE, stop).
init([]) ->
{ok, Listen} = gen_tcp:listen(12500, [{active, false}, {reuseaddr, true}]),
{ok, #state{listen_socket = Listen}}.
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({tcp, Socket, Data}, State) ->
spawn(fun() -> handle_request(Socket, Data) end),
{noreply, State};
handle_info(stop, State) ->
{stop, normal, ok, State};
handle_info(_, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
start_listener(Listen) ->
{ok, Socket} = gen_tcp:accept(Listen),
gen_tcp:controlling_process(Socket, self()),
accept(Socket).
accept(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
spawn(fun() -> handle_request(Socket, Data) end),
accept(Socket);
{error, _} ->
ok
end.
handle_request(Socket, Data) ->
case http_parser:parse_request(Data) of
{ok, Request} ->
case Request#http_request.method of
'GET' ->
handle_get_request(Socket, Request);
'POST' ->
handle_post_request(Socket, Request);
'DELETE' ->
handle_delete_request(Socket, Request);
_ ->
send_error_response(Socket, 400, "Bad Request", "Invalid method")
end;
{error, Reason} ->
send_error_response(Socket, 400, "Bad Request", Reason)
end,
gen_tcp:close(Socket).
handle_get_request(Socket, Request) ->
Filepath = Request#http_request.uri,
case file:read_file(Filepath) of
{ok, Data} ->
send_response(Socket, 200, "OK", Data);
{error, enoent} ->
send_error_response(Socket, 404, "Not Found", "File not found");
_ ->
send_error_response(Socket, 500, "Internal Server Error", "Failed to read file")
end.
handle_post_request(Socket, Request) ->
{_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, Headers, Body} = Request,
case proplists:get_value("content-type", Headers) of
"image/jpeg" ->
case file:write_file("file.jpg", Body) of
ok ->
send_response(Socket, 201, "Created", "");
_ ->
send_error_response(Socket, 500, "Internal Server Error", "Failed to write file")
end;
_ ->
send_error_response(Socket, 500, "Internal Server Error", "Invalid file type")
end.
handle_delete_request(Socket, Request) ->
Filepath = Request#http_request.uri,
case file:delete(Filepath) of
ok ->
send_response(Socket, 204, "No Content", "");
_ ->
send_error_response(Socket, 500, "Internal Server Error", "Failed to delete file")
end.
send_response(Socket, Status, Reason, Body) ->
Response = http_parser:format_response(#http_response{status = Status, reason = Reason}, Body),
gen_tcp:send(Socket, Response).
send_error_response(Socket, Status, Reason, Body) ->
Response = http_parser:format_response(#http_response{status = Status, reason = Reason}, Body),
gen_tcp:send(Socket, Response).
```
以上代码实现了一个简单的 HTTP 服务器,可以通过 GET 方法获取文件内容,通过 POST 方法上传 JPG 文件,通过 DELETE 方法删除文件。其中,GET 方法会返回 404 错误,POST 方法会返回 500 错误,DELETE 方法会返回 500 错误,如果文件不存在或文件类型不正确的话。
阅读全文