@@ -25,7 +25,8 @@ static void * ngx_rtmp_hls_create_app_conf(ngx_conf_t *cf);
25
25
static char * ngx_rtmp_hls_merge_app_conf (ngx_conf_t * cf ,
26
26
void * parent , void * child );
27
27
static ngx_int_t ngx_rtmp_hls_flush_audio (ngx_rtmp_session_t * s );
28
- static ngx_int_t ngx_rtmp_hls_ensure_directory (ngx_rtmp_session_t * s );
28
+ static ngx_int_t ngx_rtmp_hls_ensure_directory (ngx_rtmp_session_t * s ,
29
+ ngx_str_t * path );
29
30
30
31
31
32
#define NGX_RTMP_HLS_BUFSIZE (1024*1024)
@@ -846,15 +847,22 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
846
847
ngx_rtmp_hls_app_conf_t * hacf ;
847
848
848
849
ctx = ngx_rtmp_get_module_ctx (s , ngx_rtmp_hls_module );
850
+
849
851
if (ctx -> opened ) {
850
852
return NGX_OK ;
851
853
}
852
854
853
- if (ngx_rtmp_hls_ensure_directory (s ) != NGX_OK ) {
855
+ hacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_hls_module );
856
+
857
+ if (ngx_rtmp_hls_ensure_directory (s , & hacf -> path ) != NGX_OK ) {
854
858
return NGX_ERROR ;
855
859
}
856
860
857
- hacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_hls_module );
861
+ if (hacf -> keys &&
862
+ ngx_rtmp_hls_ensure_directory (s , & hacf -> keys_path ) != NGX_OK )
863
+ {
864
+ return NGX_ERROR ;
865
+ }
858
866
859
867
id = ngx_rtmp_hls_get_fragment_id (s , ts );
860
868
@@ -1096,51 +1104,53 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
1096
1104
1097
1105
1098
1106
static ngx_int_t
1099
- ngx_rtmp_hls_ensure_directory (ngx_rtmp_session_t * s )
1107
+ ngx_rtmp_hls_ensure_directory (ngx_rtmp_session_t * s , ngx_str_t * path )
1100
1108
{
1101
1109
size_t len ;
1102
1110
ngx_file_info_t fi ;
1103
1111
ngx_rtmp_hls_ctx_t * ctx ;
1104
1112
ngx_rtmp_hls_app_conf_t * hacf ;
1105
1113
1106
- static u_char path [NGX_MAX_PATH + 1 ];
1114
+ static u_char zpath [NGX_MAX_PATH + 1 ];
1107
1115
1108
1116
hacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_hls_module );
1109
1117
1110
- * ngx_snprintf (path , sizeof (path ) - 1 , "%V" , & hacf -> path ) = 0 ;
1118
+ if (path -> len + 1 > sizeof (zpath )) {
1119
+ ngx_log_error (NGX_LOG_ERR , s -> connection -> log , 0 , "hls: too long path" );
1120
+ return NGX_ERROR ;
1121
+ }
1122
+
1123
+ ngx_snprintf (zpath , sizeof (zpath ), "%V%Z" , path );
1111
1124
1112
- if (ngx_file_info (path , & fi ) == NGX_FILE_ERROR ) {
1125
+ if (ngx_file_info (zpath , & fi ) == NGX_FILE_ERROR ) {
1113
1126
1114
1127
if (ngx_errno != NGX_ENOENT ) {
1115
1128
ngx_log_error (NGX_LOG_ERR , s -> connection -> log , ngx_errno ,
1116
- "hls: " ngx_file_info_n " failed on '%V'" ,
1117
- & hacf -> path );
1129
+ "hls: " ngx_file_info_n " failed on '%V'" , path );
1118
1130
return NGX_ERROR ;
1119
1131
}
1120
1132
1121
1133
/* ENOENT */
1122
1134
1123
- if (ngx_create_dir (path , NGX_RTMP_HLS_DIR_ACCESS ) == NGX_FILE_ERROR ) {
1135
+ if (ngx_create_dir (zpath , NGX_RTMP_HLS_DIR_ACCESS ) == NGX_FILE_ERROR ) {
1124
1136
ngx_log_error (NGX_LOG_ERR , s -> connection -> log , ngx_errno ,
1125
- "hls: " ngx_create_dir_n " failed on '%V'" ,
1126
- & hacf -> path );
1137
+ "hls: " ngx_create_dir_n " failed on '%V'" , path );
1127
1138
return NGX_ERROR ;
1128
1139
}
1129
1140
1130
1141
ngx_log_debug1 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
1131
- "hls: directory '%V' created" , & hacf -> path );
1142
+ "hls: directory '%V' created" , path );
1132
1143
1133
1144
} else {
1134
1145
1135
1146
if (!ngx_is_dir (& fi )) {
1136
1147
ngx_log_error (NGX_LOG_ERR , s -> connection -> log , 0 ,
1137
- "hls: '%V' exists and is not a directory" ,
1138
- & hacf -> path );
1148
+ "hls: '%V' exists and is not a directory" , path );
1139
1149
return NGX_ERROR ;
1140
1150
}
1141
1151
1142
1152
ngx_log_debug1 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
1143
- "hls: directory '%V' exists" , & hacf -> path );
1153
+ "hls: directory '%V' exists" , path );
1144
1154
}
1145
1155
1146
1156
if (!hacf -> nested ) {
@@ -1149,44 +1159,49 @@ ngx_rtmp_hls_ensure_directory(ngx_rtmp_session_t *s)
1149
1159
1150
1160
ctx = ngx_rtmp_get_module_ctx (s , ngx_rtmp_hls_module );
1151
1161
1152
- len = hacf -> path . len ;
1153
- if (hacf -> path . data [len - 1 ] == '/' ) {
1162
+ len = path -> len ;
1163
+ if (path -> data [len - 1 ] == '/' ) {
1154
1164
len -- ;
1155
1165
}
1156
1166
1157
- * ngx_snprintf (path , sizeof (path ) - 1 , "%*s/%V" , len , hacf -> path .data ,
1158
- & ctx -> name ) = 0 ;
1167
+ if (len + 1 + ctx -> name .len + 1 > sizeof (zpath )) {
1168
+ ngx_log_error (NGX_LOG_ERR , s -> connection -> log , 0 , "hls: too long path" );
1169
+ return NGX_ERROR ;
1170
+ }
1171
+
1172
+ ngx_snprintf (zpath , sizeof (zpath ) - 1 , "%*s/%V%Z" , len , path -> data ,
1173
+ & ctx -> name );
1159
1174
1160
- if (ngx_file_info (path , & fi ) != NGX_FILE_ERROR ) {
1175
+ if (ngx_file_info (zpath , & fi ) != NGX_FILE_ERROR ) {
1161
1176
1162
1177
if (ngx_is_dir (& fi )) {
1163
1178
ngx_log_debug1 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
1164
- "hls: directory '%s' exists" , path );
1179
+ "hls: directory '%s' exists" , zpath );
1165
1180
return NGX_OK ;
1166
1181
}
1167
1182
1168
1183
ngx_log_error (NGX_LOG_ERR , s -> connection -> log , 0 ,
1169
- "hls: '%s' exists and is not a directory" , path );
1184
+ "hls: '%s' exists and is not a directory" , zpath );
1170
1185
1171
1186
return NGX_ERROR ;
1172
1187
}
1173
1188
1174
1189
if (ngx_errno != NGX_ENOENT ) {
1175
1190
ngx_log_error (NGX_LOG_ERR , s -> connection -> log , ngx_errno ,
1176
- "hls: " ngx_file_info_n " failed on '%s'" , path );
1191
+ "hls: " ngx_file_info_n " failed on '%s'" , zpath );
1177
1192
return NGX_ERROR ;
1178
1193
}
1179
1194
1180
1195
/* NGX_ENOENT */
1181
1196
1182
- if (ngx_create_dir (path , NGX_RTMP_HLS_DIR_ACCESS ) == NGX_FILE_ERROR ) {
1197
+ if (ngx_create_dir (zpath , NGX_RTMP_HLS_DIR_ACCESS ) == NGX_FILE_ERROR ) {
1183
1198
ngx_log_error (NGX_LOG_ERR , s -> connection -> log , ngx_errno ,
1184
- "hls: " ngx_create_dir_n " failed on '%s'" , path );
1199
+ "hls: " ngx_create_dir_n " failed on '%s'" , zpath );
1185
1200
return NGX_ERROR ;
1186
1201
}
1187
1202
1188
1203
ngx_log_debug1 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
1189
- "hls: directory '%s' created" , path );
1204
+ "hls: directory '%s' created" , zpath );
1190
1205
1191
1206
return NGX_OK ;
1192
1207
}
0 commit comments