From 7cbf6068b8214a89b75e3aa26bdb10d22e97ab90 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Sun, 20 Apr 2008 22:27:11 +0000 Subject: Change uuid generaion to rely on crypto. Started crypto and inets application in startscript, and also couch_server_up. Fix for regression caused by accidental check-in of disabled etags. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@649988 13f79535-47bb-0310-9956-ffa450edef68 --- bin/couchdb.tpl.in | 4 ++++ src/couchdb/couch.app.tpl.in | 2 +- src/couchdb/couch_erl_driver.c | 19 ++++--------------- src/couchdb/couch_httpd.erl | 8 ++++---- src/couchdb/couch_rep.erl | 8 -------- src/couchdb/couch_server_sup.erl | 5 +++++ src/couchdb/couch_util.erl | 5 ++--- 7 files changed, 20 insertions(+), 31 deletions(-) diff --git a/bin/couchdb.tpl.in b/bin/couchdb.tpl.in index 429508ba..4002b006 100644 --- a/bin/couchdb.tpl.in +++ b/bin/couchdb.tpl.in @@ -224,7 +224,11 @@ start_couchdb () { %ERL% $interactive_option -sasl errlog_type error \ -pa %erlanglibdir%/couch-%version%/ebin \ %erlanglibdir%/mochiweb-r64/ebin \ + -eval \"application:load(inets)\" \ + -eval \"application:load(crypto)\" \ -eval \"application:load(couch)\" \ + -eval \"crypto:start()\" \ + -eval \"inets:start()\" \ -eval \"couch_server:start(), receive done -> done end.\" \ -couchini $INI_FILE" if test "$BACKGROUND_BOOLEAN" = "true" \ diff --git a/src/couchdb/couch.app.tpl.in b/src/couchdb/couch.app.tpl.in index 95bc6708..3abc6302 100644 --- a/src/couchdb/couch.app.tpl.in +++ b/src/couchdb/couch.app.tpl.in @@ -25,4 +25,4 @@ couch_view, couch_query_servers, couch_ft_query]}, - {applications,[kernel,stdlib,xmerl,inets,mochiweb]}]}. + {applications,[kernel,stdlib,crypto,inets,mochiweb]}]}. diff --git a/src/couchdb/couch_erl_driver.c b/src/couchdb/couch_erl_driver.c index 550cff9e..c6541eaa 100644 --- a/src/couchdb/couch_erl_driver.c +++ b/src/couchdb/couch_erl_driver.c @@ -23,7 +23,6 @@ specific language governing permissions and limitations under the License. #ifndef WIN32 #include // for memcpy #endif -#include typedef struct { ErlDrvPort port; @@ -91,21 +90,11 @@ static int return_control_result(void* pLocalResult, int localLen, char **ppRetB static int couch_drv_control(ErlDrvData drv_data, unsigned int command, const char *pBuf, int bufLen, char **rbuf, int rlen) { - #define COLLATE 0 - #define COLLATE_NO_CASE 1 - #define UUID 2 couch_drv_data* pData = (couch_drv_data*)drv_data; switch(command) { - case UUID: - { - uuid_t uuid; - uuid_generate(uuid); - return return_control_result(&uuid, sizeof(uuid), rbuf, rlen); - } - - case COLLATE: - case COLLATE_NO_CASE: + case 0: // COLLATE + case 1: // COLLATE_NO_CASE: { UErrorCode status = U_ZERO_ERROR; int collResult; @@ -134,9 +123,9 @@ static int couch_drv_control(ErlDrvData drv_data, unsigned int command, const ch // point the iterator at it. uiter_setUTF8(&iterB, pBuf, length); - if (command == COLLATE) + if (command == 0) // COLLATE collResult = ucol_strcollIter(pData->coll, &iterA, &iterB, &status); - else if (command == COLLATE_NO_CASE) + else // COLLATE_NO_CASE collResult = ucol_strcollIter(pData->collNoCase, &iterA, &iterB, &status); if (collResult < 0) diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 31bdd143..9351dc89 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -437,7 +437,7 @@ handle_doc_request(Req, 'GET', _DbName, Db, DocId) -> JsonDoc = couch_doc:to_json_obj(Doc, Options), AdditionalHeaders = case Doc#doc.meta of - [] -> [{"XEtag", Etag}]; % output etag when we have no meta + [] -> [{"Etag", Etag}]; % output etag when we have no meta _ -> [] end, send_json(Req, 200, AdditionalHeaders, JsonDoc); @@ -504,7 +504,7 @@ handle_doc_request(Req, 'PUT', _DbName, Db, DocId) -> Doc = couch_doc:from_json_obj(Json), {ok, NewRev} = couch_db:update_doc(Db, Doc#doc{id=DocId, revs=Revs}, []), - send_json(Req, 201, [{"XEtag", "\"" ++ NewRev ++ "\""}], {obj, [ + send_json(Req, 201, [{"Etag", "\"" ++ NewRev ++ "\""}], {obj, [ {ok, true}, {id, DocId}, {rev, NewRev} @@ -797,9 +797,9 @@ error_to_json0(Error) -> send_error(Req, {method_not_allowed, Methods}) -> {ok, Req:respond({405, [{"Allow", Methods}], <<>>})}; send_error(Req, {modified, Etag}) -> - {ok, Req:respond({412, [{"XEtag", Etag}], <<>>})}; + {ok, Req:respond({412, [{"Etag", Etag}], <<>>})}; send_error(Req, {not_modified, Etag}) -> - {ok, Req:respond({304, [{"XEtag", Etag}], <<>>})}; + {ok, Req:respond({304, [{"Etag", Etag}], <<>>})}; send_error(Req, Error) -> {Code, Json} = error_to_json(Error), ?LOG_INFO("HTTP Error (code ~w): ~p", [Code, Error]), diff --git a/src/couchdb/couch_rep.erl b/src/couchdb/couch_rep.erl index df0a4da1..bead047c 100644 --- a/src/couchdb/couch_rep.erl +++ b/src/couchdb/couch_rep.erl @@ -155,14 +155,6 @@ do_http_request(Url, Action, JsonBody) -> ?LOG_DEBUG("\tAction: ~p", [Action]), ?LOG_DEBUG("\tUrl: ~p", [Url]), - % ensure that the inets application is running - case application:start(inets) of - ok -> - ok; - {error, {already_started, inets}} -> - ok - end, - Request = case JsonBody of [] -> diff --git a/src/couchdb/couch_server_sup.erl b/src/couchdb/couch_server_sup.erl index 9117f5f8..6fed6223 100644 --- a/src/couchdb/couch_server_sup.erl +++ b/src/couchdb/couch_server_sup.erl @@ -145,6 +145,11 @@ start_server(InputIniFilename) -> io:format("couch ~s (LogLevel=~s)~n", [couch_server:get_version(), LogLevel]), io:format("~s~n", [ConsoleStartupMsg]), + + + % ensure these applications are running + application:start(inets), + application:start(crypto), process_flag(trap_exit, true), StartResult = (catch supervisor:start_link( diff --git a/src/couchdb/couch_util.erl b/src/couchdb/couch_util.erl index 504a675a..94648b0d 100644 --- a/src/couchdb/couch_util.erl +++ b/src/couchdb/couch_util.erl @@ -40,7 +40,7 @@ start_link(LibDir) -> new_uuid() -> - to_hex(erlang:port_control(drv_port(), 2, <<>>)). + to_hex(binary_to_list(crypto:rand_bytes(16))). to_hex([]) -> []; @@ -57,8 +57,7 @@ to_digit(N) -> % returns a random integer rand32() -> - [A,B,C,D|_] = erlang:port_control(drv_port(), 2, <<>>), - (A bsl 24) + (B bsl 16) + (C bsl 8) + D. + crypto:rand_uniform(0, 16#100000000). % given a pathname "../foo/bar/" it gives back the fully qualified % absolute pathname. -- cgit v1.2.3